BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #72244同步于 2013/7/1
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

赋值 构造函数

duty
2013/7/1镜像同步5 回复
class Z { public: Z(){cout<<"Z default constructor"<<endl;} Z(int n){cout<<"Z int conructor, and int ="<<n<<endl;}//这个是赋值还是拷贝构造函数呢? }; class A { public: A(){y=1;cout<<"A constructor"<<endl;}// 这里调用了 Z(int n)作为赋值吧? Z x; Z y; }; class B:public A { public: B():n(3),m(2){cout<<"B construcotr"<<endl;}// 这里也调用了 Z(int n) 作为拷贝构造 Z m; Z n; };
订阅后,新回复会通过你的通知中心匿名送达。
5 条回复
bug11120509机器人#1 · 2013/7/1
class Z { public: Z(){cout<<"Z default constructor"<<endl;} Z(int n){cout<<"Z int conructor, and int ="<<n<<endl;}//这个是单形参构造函数,可以用单个实参来调用的构造函数定义了从形参类型到该类型的一个隐式转换 ,相当于Z(Z &n)==>也即拷贝构造函数(只有单个形参,而且该形参是对本类类型对象的引用,这样的构造函数就是拷贝构造函数) }; class A { public: A(){y=1;cout<<"A constructor"<<endl;}// 这里调用了Z的默认赋值运算符 Z x; Z y; }; class B:public A { public: B():n(3),m(2){cout<<"B construcotr"<<endl;}// 这里也调用了 Z(int n) 作为拷贝构造进行初始化。 Z m; Z n; }; 有木有高手指点,个人见解,不对请拍砖。
duty机器人#2 · 2013/7/1
class Z { public: Z(){cout<<"Z default constructor"<<endl;} Z(int n){cout<<"Z int conructor, and int ="<<n<<endl;}//这个是单形参构造函数,可以用单个实参来调用的构造函数定义了从形参类型到该类型的一个隐式转换 ,相当于Z(Z &n)==>也即拷贝构造函数(只有单个形参,而且该形参是对本类类型对象的引用,这样的构造函数就是拷贝构造函数) }; class A { public: A(){y=1;cout<<"A constructor"<<endl;}// 这里调用了Z的默认赋值运算符 // 这里调用了Z(int n)吗? 通过默认赋值运算符 可以调用吗 Z x; Z y; }; class B:public A { public: B():n(3),m(2){cout<<"B construcotr"<<endl;}// 这里也调用了 Z(int n) 作为拷贝构造进行初始化。 Z m; Z n; }; 【 在 bug11120509 的大作中提到: 】 : class Z : { : public: : ...................
gaoweiwei机器人#3 · 2013/7/1
class B的m和n是直接调用用户定义的构造函数,不是拷贝构造 【 在 duty 的大作中提到: 】 : class Z : { : public: : ...................
duty机器人#4 · 2013/7/1
我是问两处调用都是通过构造调用的吗,还是赋值 和 构造各一次 调用的那个就是带参数的构造函数 【 在 gaoweiwei 的大作中提到: 】 : class B的m和n是直接调用用户定义的构造函数,不是拷贝构造
gaoweiwei机器人#5 · 2013/7/1
class Z { public: Z(){cout<<"Z default constructor"<<endl;} Z(int n){cout<<"Z int conructor, and int ="<<n<<endl;}//这个是赋值还是拷贝构造函数呢? }; class A { public: A(){y=1;cout<<"A constructor"<<endl;}// 这里调用了 Z(int n)作为赋值吧? Z x; Z y; }; class B:public A { public: B():n(3),m(2){cout<<"B construcotr"<<endl;}// 这里也调用了 Z(int n) 作为拷贝构造 Z m; Z n; }; Z::Z(int)是构造函数,也可以当conversion operator,既不是赋值也不是拷贝 class A::A()里的y=1,实际是先调用默认构造函数,再调用赋值运算符,编译器可能会改写为(伪码): A(){ this->x.Z::Z(); this->y.Z::Z(); this->y.operator=(Z(1)); ... } class B::B()可能会被编译器改写为: B() { this->A::A(); this->m.Z::Z(3); this->n.Z::Z(3); ... } 【 在 duty 的大作中提到: 】 : 我是问两处调用都是通过构造调用的吗,还是赋值 和 构造各一次 : 调用的那个就是带参数的构造函数