R
RainBow
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
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