关于 Constructor&Destructor

kwib799 2003-11-30 05:58:48
请问Constructor&Destructor是不是必须一一对应的,也就是说没调用一次CON就得调用一次DES,如果是的话,下面的程序哪位给解释一下。为什么只调用了一次CON后却调用了3次DES。

#include <fstream>
#include <string>
using namespace std;
ofstream out("HowMany.out");

class HowMany {
static int objectCount;
public:
HowMany() { objectCount++;out<<"Constructor called!"<<endl;}
static void print(const string& msg = "") {
if(msg.size() != 0) out << msg << ": ";
out << "objectCount = "
<< objectCount << endl;
}
~HowMany() {
objectCount--;
print("~HowMany()");
}
};

int HowMany::objectCount = 0;

// Pass and return BY VALUE:
HowMany f(HowMany x) {
x.print("x argument inside f()");
return x;
}

int main() {
HowMany h;
HowMany::print("after construction of h");
HowMany h2 = f(h);
HowMany::print("after call to f()");
} ///:~
...全文
44 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
kwib799 2003-11-30
  • 打赏
  • 举报
回复
会不会涉及到对象之间的操作调用CC和OPERATOR OVERLOADING时编译器会选择CC
kahn 2003-11-30
  • 打赏
  • 举报
回复
运算符重载,自定义的某个运算符
abitz 2003-11-30
  • 打赏
  • 举报
回复
我以为能调用它那。
不过结果不是这样。
为什么哪?
kwib799 2003-11-30
  • 打赏
  • 举报
回复
您在这里加进一个 OPERATOR OVERLOADING 是什么意思?
abitz 2003-11-30
  • 打赏
  • 举报
回复
运行结果:
Constructor called!
after construction of h: objectCount = 1
Copy Ctor called! // f(h),pass by value,利用copy ctor产生临时对象
x argument inside f(): objectCount = 2
Copy Ctor called! // h2 = f(h);不是operator=,而是copy ctor;
~HowMany(): objectCount = 2 // 临时对象析构
after call to f(): objectCount = 2
~HowMany(): objectCount = 1
~HowMany(): objectCount = 0
abitz 2003-11-30
  • 打赏
  • 举报
回复
改成这样就好了:
#include <fstream>
#include <string>
using namespace std;
ofstream out("HowMany.out");

class HowMany {
static int objectCount;
public:
HowMany() { objectCount++;out<<"Constructor called!"<<endl;}
HowMany(const HowMany& x) {objectCount++; out<<"Copy Ctor called!"<<endl;}
HowMany& operator=(const HowMany& x)
{objectCount++;out<<"operator = called!"<<endl;
return *this;}
static void print(const string& msg = "")
{
if(msg.size() != 0) out << msg << ": ";
out << "objectCount = "
<< objectCount << endl;
}
~HowMany() {
objectCount--;
print("~HowMany()");
}
};

int HowMany::objectCount = 0;

// Pass and return BY VALUE:
HowMany f(HowMany x) {
x.print("x argument inside f()");
return x;
}

int main() {
HowMany h;
HowMany::print("after construction of h");
HowMany h2 = f(h); // h2 = f(h)这里没有调用operator=,而是copy ctor.
// 不解
HowMany::print("after call to f()");
} ///:~
abitz 2003-11-30
  • 打赏
  • 举报
回复
int main() {
HowMany h; // default ctor called
HowMany::print("after construction of h");
HowMany h2 = f(h); // f(h):copy ctor called, when returning, dtor called;
// h2 = f(h); default operator = called;
HowMany::print("after call to f()");
} // at the end dtor of h and h2 called
// total dtor called: 3 times

65,203

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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