如题,有下面一段代码:
#include <iostream>
using namespace std;
class Person{
public:
Person(){
cout << "default constructor is called!" << endl;
}
Person(int n)
{
num = n;
cout << "constructor is called!" << n <<endl;
}
//explicit Person(const Person& pp)
Person(const Person& pp)
{
num = pp.num + 3;
cout << "Copy constructor is called!" << endl;
}
void display()
{
cout << "num = " << num << endl;
}
//Person operator=(Person& pp)
//{
// cout << "Assignment constructor is called!" << endl;
// return p;
//}
private:
int num;
};
int main()
{
Person p = Person(10);
//Person p(Person(10));
//Person p = pp;
p.display();
return 0;
}
打印结果是:constructor is called!
num = 10
如果将拷贝构造函数改为explicit,这编译有错。
但如果调这个拷贝构造,为什么它里面的不打印,且num的值也不是13而是10。 |