 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
RainBow Guest
|
Posted: Thu May 17, 2007 9:11 am Post subject: Temporary & default c'tor |
|
|
I understand that a compiler synthesises a default constructor if none
is provided by the user ( of course depending on the situation if
synthesis of such c'tor is actually needed in the program e.g if vptr
is needed, default c'tor must be synthesised by compiler).
In the class Temporary below, there is no reason why a compiler should
synthesise a default c'tor.
class Temporary
{
public:
Temporary(const Temporary& a) { printf("copy ctor of Temporary invoked
\n"); }
Temporary(int a, int b){printf("in user defined ctor\n");}
~Temporary() { printf("in dtor of Temporary\n");}
};
Now, if I write a function like:
Temporary induceTemporaryGeneration()
{
Temporary t(8,9);
return t;
}
I get the output as:
[
in user defined ctor
copy ctor of Temporary invoked
in dtor of Temporary
in dtor of Temporary
]
In this scenario, I know that dtor is called twice because of a
temporary that is created by compiler, but what I dont understand is :
What type of CONSTRUCTOR does compiler use to create this temporary? I
have not provided any default c'tor here so I was hoping that compiler
will give me an error "No default c'tor found in class Temporary", but
this does not happen in Visual Studio 8 VC++ compiler. Why? Why? Why?
I thought of what would be going behind the scene based on the output
generated, and this is what I think might be happening: Please let me
know if my understanding is correct or not.
//Does compiler change it like this?
Temporary induceTemporaryGeneration( const Temporary& aTmp )
{
Temporary t(8,9); // in user defined ctor
aTmp.Temporary::Temporary(t); //COMPILER ADDED?
return t;
}
//Does compiler augment the function like this?
void test_Temporary()
{
Temporary tmp;
tmp.Temporary::Temporary(); //Which constructor will be run on tmp
here?
Temporary f = induceTemporaryGeneration(tmp);
f.Temporary::Temporary(tmp); // copy ctor of Temporary invoked
tmp.Temporary::~Temporary(); // in dtor of Temporary
f.Temporary::~Temporary(); // in dtor of Temporary
}
Thanks for your replies.
~Viren |
|
| Back to top |
|
 |
Erik Wikström Guest
|
Posted: Thu May 17, 2007 9:11 am Post subject: Re: Temporary & default c'tor |
|
|
On 2007-05-17 08:27, RainBow wrote:
| Quote: | I understand that a compiler synthesises a default constructor if none
is provided by the user ( of course depending on the situation if
synthesis of such c'tor is actually needed in the program e.g if vptr
is needed, default c'tor must be synthesised by compiler).
In the class Temporary below, there is no reason why a compiler should
synthesise a default c'tor.
class Temporary
{
public:
Temporary(const Temporary& a) { printf("copy ctor of Temporary invoked
\n"); }
Temporary(int a, int b){printf("in user defined ctor\n");}
~Temporary() { printf("in dtor of Temporary\n");}
};
Now, if I write a function like:
Temporary induceTemporaryGeneration()
{
Temporary t(8,9);
return t;
}
I get the output as:
[
in user defined ctor
copy ctor of Temporary invoked
in dtor of Temporary
in dtor of Temporary
]
In this scenario, I know that dtor is called twice because of a
temporary that is created by compiler, but what I dont understand is :
What type of CONSTRUCTOR does compiler use to create this temporary?
|
Your output is quite clear, first an object is created using you user-
defined ctor, then a copy of that one is made using the copy-ctor. Both
of those objects are then destructed. No ctor other than those that you
have defined are used. The copy is made because you are returning t by
value, so a copy is made in the function that called
induceTemporaryGeneration().
Notice that as soon as you define *any* ctor the compiler will refrain
from creating default ones. So if you create a class with just a copy
ctor then you can't create an instance of this class since there's no
normal ctro.
--
Erik Wikström |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|