A question about constructor function
class T
{
public:
int m_i ;
T () { m_i = 1 ; }
T(int k ) { T() ; }
};
int main()
{
T t(0 );
cout<< t.m_i <<endl;
return 0;
}
When i invoke a constructor funciton within another constructor funciton , this constructor will not impact on this object .
In the above sample code , The t.m_i will remain uninitialize . why
? i test it in VC6.
I have gone throught the ISO C++ specificaiton , but no any thread was been found .
Re: A question about constructor function
Quote:
Originally posted by flysnow
When i invoke a constructor funciton within another constructor funciton , this constructor will not impact on this object .
In the above sample code , The t.m_i will remain uninitialize . why
? i test it in VC6.
The reason is quite simple...the call to the first constructor inside your second one will create a temporar instance of 'T' which is only valid for the current scope thus only until the second constructor returns...
You can easily see what happens while debugging through the construction of the instance of 'T'...take a look at the 'this' pointers...the following code demonstrates the problem as well...just run it and look at the output...
Code:
#include <iostream>
#include <iomanip>
class T
{
public:
int m_i ;
T()
{
std::cout << "First constructor = 0x" << std::hex << this << std::endl;
m_i = 1 ;
}
T(int k)
{
std::cout << "Second constructor = 0x" << std::hex << this << std::endl;
T();
}
~T()
{
std::cout << "Destructor = 0x" << std::hex << this << std::endl;
}
};
int main()
{
{
T t(0);
std::cout << "Instance created" << std::endl;
}
return 0;
}
Are there some c++ expert here ?
I know the result , but why? why was the temporary object created ?
Is it a normal behavior defined by C++ specificaiton ?
Thanks Mikey and Andreas Masur .
The code is just for describing the problem . so i put the m_i public. :)
clarifications and focusings
** galathaea's thoughts on reading :
This is getting a little scattered, so let's try to focus on the points being made and try to understand what confusion is happening here. It could be mine, but...
creates what is known as an anonymous instance of the class. This is a common idiom that does not need anymore expert advice than what has been given (Andreas and Mikey are two of the smartest posters here on CodeGuru in my opinion...). One common use is in a form of object factory (not common today, but at one point it was used rather widespread) where one creates symbol classes, ie. classes that have a name only (no members, basically a string precompiled into a more efficient form) as the overloaded parameter to decide which factory function to call.
Now a destructor gets called on every object created. So it depends on what objects are created. One can return a temporary object that then gets assigned into a given variable, something like
Code:
MyObject = MyFunc(...);
or copied
Code:
MyClassType MyObject(MyFunc(...));
or
MyClassType MyObject = MyFunc(...);
but either way, the destructor is fired when the temp object finishes the sequence point it is attached to. So it is not only possible but quite easy for the temp object to last long enough for the copy or assignment, but no longer.
Any anonymous temporary object is not found in the same logical memory as the instantiated objects of the full code block (the c++ "stack", not necessarily related to a computers execution stack, though I know of no case where they are different). Instead, logically to the compiler, it exists only for the duration of the sequence points it encompasses. Everything else occurs through argument passing steps, which invoke copies or references. These argument passing steps may be of whatever function is called attached to the relevant sequence points, including operator=.
At least, this is how I understand things. If I err, there are plenty intelligent posters on these boards that will clean up my language...
Re: clarifications and focusings
Quote:
Originally posted by galathaea
creates what is known as an anonymous instance of the class. This is a common idiom that does not need anymore expert advice than what has been given (Andreas and Mikey are two of the smartest posters here on CodeGuru in my opinion...).
Yes , it will create anonymous instance .. but what we is discussing is why C++ need this feature ? when this feature can be use ?and why don't need scope like T::T() when you call the construtor ?
If a global funciton name is the same name with class T, Global function will be called instead of the class construtor function . This is a strange behavior .
Quote:
One common use is in a form of object factory (not common today, but at one point it was used rather widespread) where one creates symbol classes, ie. classes that have a name only (no members, basically a string precompiled into a more efficient form) as the overloaded parameter to decide which factory function to call.
I am not very clear about what you said . Can you explain it more detai? Thanks in advance .