P
peifeng_w
Hi, try the following code with flag=0/1/2.
#include<iostream>
using namespace std;
#define flag 2//option:0,1,2
class C
{
public:
C(){cout<<"C"<<endl;}
#if flag==1
C(C&){cout<<"C"<<endl;}
#endif
virtual ~C(){cout<<"~C"<<endl;}
};
#if flag==0
void myfunc(C)
{
}
#elif flag==1
void myfunc(C)
{
}
#elif flag==2
void myfunc(C&)
{
}
#endif
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"flag="<<flag<<endl;
C myC;
myfunc(myC);
return 0;
}
/*
result:
flag=0
C
~C
~C
flag=1
C
C
~C
~C
flag=2
C
~C
*/
It really surprized me that when flag=0, I got C called once with ~C
called twice. I know that if more ctor are called than dtor, then there
is a memory leak. But what happened when more dtor is called than ctor?
Does anybody know relevant specification in C++ standard? I am using
VS.net 2003. Thanks.
#include<iostream>
using namespace std;
#define flag 2//option:0,1,2
class C
{
public:
C(){cout<<"C"<<endl;}
#if flag==1
C(C&){cout<<"C"<<endl;}
#endif
virtual ~C(){cout<<"~C"<<endl;}
};
#if flag==0
void myfunc(C)
{
}
#elif flag==1
void myfunc(C)
{
}
#elif flag==2
void myfunc(C&)
{
}
#endif
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"flag="<<flag<<endl;
C myC;
myfunc(myC);
return 0;
}
/*
result:
flag=0
C
~C
~C
flag=1
C
C
~C
~C
flag=2
C
~C
*/
It really surprized me that when flag=0, I got C called once with ~C
called twice. I know that if more ctor are called than dtor, then there
is a memory leak. But what happened when more dtor is called than ctor?
Does anybody know relevant specification in C++ standard? I am using
VS.net 2003. Thanks.