返回信息流#include <cstdlib>
#include <iostream>
using namespace std;
class tt
{
int m_tt;
public:
tt() {cout<<"tt::tt()"<<endl;}
tt(int i) {cout<<"d:int"<<endl;}
tt(tt& obj) {cout<<"d:copy"<<endl; }
tt operator=(tt& obj) {cout<<"d:assign"<<endl; return obj;}
};
class dd
{
int m_a;
tt m_tt1;
tt m_tt2;
public:
dd(tt& t1, tt& tt2): m_a(1),m_tt1(t1)
{
this->m_tt2 = tt2;
cout<<"dd:dd()"<<endl;
}
};
int main(int argc, char *argv[])
{
tt t1;
tt t2;
dd obj(t1,t2);
system("PAUSE");
return EXIT_SUCCESS;
}
输出结果为:
tt::tt() //创建t1
tt::tt() //创建t2
tt:copy //初始化m_t1
tt::tt() //初始化m_tt2吧
tt:assign //然后调用赋值构造函数
tt:copy // 那这句是为什么为什么会有呢?
dd:dd() //dd构造函数输出的
请按任意键继续. . .
这是一条镜像帖。来源:北邮人论坛 / cpp / #80763同步于 2014/7/10
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
关于构造函数的参数列表。
youziboy
2014/7/10镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
运算符重载返回一个tt类型的对象 拷贝构造函数给m_t2初始化
【 在 youziboy (怪人) 的大作中提到: 】
: [code=c]
: #include <cstdlib>
: #include <iostream>
: ...................
哦,对,我忘记返回应该是引用了。谢谢
【 在 gsl2011 的大作中提到: 】
: return obj产生的, 正常的赋值操作赋tt &operator=(const tt &), 是返回引用的。