我在 C++ 中有以下代码
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
class A{
public:
int x;
int y;
int first(){
return x;
}
int second(){
return y;
}
};
class C{
public:
float a,b;
C(){
a = 0.0f;
b = 0.0f;
}
template<class T>
C(T t){
cout<<"Copy Constructor\n";
a = t.first();
b = t.second();
}
template<class T>
C & operator=(T const &c){
cout <<"Assignment operator\n";
this->a = c.first();
this->b = c.first();
}
};
class D: public C{
public:
template <typename T> D (T t) : C(t) {}
float area(){
return a*b;
}
};
int main(){
A a;
a.x = 6;
a.y = 8;
C c(a);
D d(a);
D e = a; // Here copy constructor is being called!!
cout<<e.a<<" "<<e.b<<" "<<e.area()<<endl;
}
这是上述程序的输出
Copy Constructor
Copy Constructor
Copy Constructor
6 8 48
为什么派生类中没有调用赋值运算符?
Edit1:我已更改问题以使问题更清楚。