关于子对象初始化问题

一aa一 2012-03-05 05:28:29
我在一个类内声明子对象string 那么这个子对象是在声明的时候创建的,还是在Student类构造函数的成员列表给string类传值的时候创建的


class Student
{
private:
string name; /// 1
public:
Student():name("aa"){} ///2
}

这个name子对象的创建是在1还是2呢? 我个人觉得应该是在2的。因为对象创建在构造函数执行之后。可是string name; 看上去也像执行了string的默认构造函数。
...全文
188 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
xxplili 2012-03-05
  • 打赏
  • 举报
回复
bobHisMother bm2(b);

调用这句话的时候运行了这些东西:
bm2:default constructor:0
assignment operator:1

前面那个是默认构造函数
后面那个是assignment operator。

就是这一句话。。 前面的都没啥用
一aa一 2012-03-05
  • 打赏
  • 举报
回复

void main(){
cout<<"bob b:";
bob b(1);

cout<<"bm1:";
bobHisMother bm1(2);

cout<<"bm2:";
bobHisMother bm2(b);
}

这里 bm1,bm2 本来就不是同一对象啊,你在上面创建了一次,后面有创建了一次,这是本来就是不同对象啊?这里能说明 我的子对象的创建时间是在什么地方么。。。
xxplili 2012-03-05
  • 打赏
  • 举报
回复
不是,关键是下面那个bm2。。。。 bm1跟bm2是两个不同的对象。。。 那个b.a=a,是在bm1里面的, 跟这个帖子关系不大。。
一aa一 2012-03-05
  • 打赏
  • 举报
回复
我觉得你的第一个code 里面 对象是在 你写 b.a = a 的时候才创建的
xxplili 2012-03-05
  • 打赏
  • 举报
回复
我的第一个程序里面有调用啊。。 你仔细看看。。 但是第二个没有
一aa一 2012-03-05
  • 打赏
  • 举报
回复
如果在1创建对象 不是应该调用默认的构造函数的么?为什么没有调用呢?
xxplili 2012-03-05
  • 打赏
  • 举报
回复
我觉得我第一段code能说明问题。
第一段code的时候他先在1处创建了对象,然后在2处赋值。。。所以先调用了default constructor,然后再调用assignment operator。
我觉得第二段程序很大程度上算是一个编译器的优化。。
一aa一 2012-03-05
  • 打赏
  • 举报
回复

#include <iostream>
using std::cout;

class testA
{
public:
testA(){cout << "Initialization a\n";}
testA(int a) {cout << "Initialization A\n";}
};
class testB
{
private:
testA testa;
public:
testB():testa(2){cout << "Initialization B\n";}
};

int main()
{
testB B;
}

我也测试了下 的确是2 处才创建对象的 那么1处他做了什么呢?
xxplili 2012-03-05
  • 打赏
  • 举报
回复
我测试了下, 给你两段代码:


#include<iostream>
using namespace std;
class bob{
public:
int a;
bob(){
a=0;
cout<<"default constructor:"<<a<<endl;
}
bob(int t){
a=t;
cout<<"argument int constructor:"<<a<<endl;
}
bob(const bob& b){
a=b.a;
cout<<"copy constructor:"<<a<<endl;
}
bob& operator =(const bob& b){
if(this==&b){
return *this;
}
a=b.a;
cout<<"assignment operator:"<<a<<endl;
return *this;
}
};

class bobHisMother{
private:
bob b;
public:
bobHisMother(int a){
b.a=a;
}
bobHisMother(const bob& bo){
b=bo;
}
};

void main(){
cout<<"bob b:";
bob b(1);

cout<<"bm1:";
bobHisMother bm1(2);

cout<<"bm2:";
bobHisMother bm2(b);
}


这个结果是:
bob b:argument int constructor:1
bm1:default constructor:0
bm2:default constructor:0
assignment operator:1


所以他是先创建了调用了类成员对象的default constructor,然后调用了assignment

但是你这个情况又貌似不同了。
你这里的构造函数是:
Student():name("aa"){}

所以另给一段代码:


#include<iostream>
using namespace std;
class bob{
public:
int a;
bob(){
a=0;
cout<<"default constructor:"<<a<<endl;
}
bob(int t){
a=t;
cout<<"argument int constructor:"<<a<<endl;
}
bob(const bob& b){
a=b.a;
cout<<"copy constructor:"<<a<<endl;
}
bob& operator =(const bob& b){
if(this==&b){
return *this;
}
a=b.a;
cout<<"assignment operator:"<<a<<endl;
return *this;
}
};

class bobHisMother{
private:
bob b;
public:
bobHisMother(int a){
b.a=a;
}
bobHisMother(const bob& bo):b(bo){
}
};

void main(){
cout<<"bob b:";
bob b(1);

cout<<"bm1:";
bobHisMother bm1(2);

cout<<"bm2:";
bobHisMother bm2(b);
}


结果是:
bob b:argument int constructor:1
bm1:default constructor:0
bm2:copy constructor:1


So, 这里他只调用了拷贝构造函数。。。 很明显编译器把它优化了下。。。
所以你那样写的话应该是1处没有创建,而是在2处创建了。
一aa一 2012-03-05
  • 打赏
  • 举报
回复
也就是说在1的时候对象已经存在, string的构造函数已经执行了是么。
xxplili 2012-03-05
  • 打赏
  • 举报
回复
而且我觉得2的地方name是调用的assignment operator
xxplili 2012-03-05
  • 打赏
  • 举报
回复
我觉得应该是先string name也就是1那个地方先初始化一次,然后再后来2的地方有进行了一次。
记得以前有做过这个类似的test

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧