|
-
November 30th, 2002, 12:34 PM
#1
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 .
-
November 30th, 2002, 02:35 PM
#2
You shouldn't call another constructor inside a constructor but a reset-function in both:
Code:
class T
{
public:
int m_i ;
T () { reset() ; }
T(int k ) { reset(); }
private:
void reset(){m_i = 1 ;}
};
(And just for design: You shouldn't put variables in the public-section, but implement get/set-functions for that purpose.)
Mikey
-
November 30th, 2002, 02:46 PM
#3
(But to Your question: I really do not know, why m_i stays uninitilized. Maybe because it's not legal to call a constructor directly.)
Mikey
-
November 30th, 2002, 02:47 PM
#4
Re: A question about constructor function
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;
}
-
December 1st, 2002, 09:51 AM
#5
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.
-
December 1st, 2002, 10:26 AM
#6
I know the result , but why? why was the temporary object created ?
Andreas is absolutely right, that You create a temporary object instead of calling Your own default-constructor.
It's really quite simple:
You create an object of T with T() (default-ctor). It doesn't matter if You write this inside or outside Your class.
You can prove this by comment out Your default-ctor-definition:
Code:
class T
{
public:
int m_i ;
// T () { m_i = 1 ; } no default-constructor!
T(int k ) { T() ; } // try to create a T-object through the default-ctor
};
int main()
{
T(); // the same: creating a T-object through default-ctor
T t(0 );
cout<< t.m_i <<endl;
return 0;
}
this raises the error:
... error C2512: 'T::T' : no appropriate default constructor
This is standard C++-behaviour
Mikey
Last edited by Mikey; December 1st, 2002 at 10:28 AM.
-
December 1st, 2002, 11:57 AM
#7
Mikey:
Thanks!
Can I called a constructor funciton directly to create a object ?
The answer is Yes by your sample code .
but why the C++ need this feature ? Because you can not access this temporary object by any way.
-
December 1st, 2002, 12:08 PM
#8
You can create an object, without saving it to a var, if You only need a constructor-execution:
Code:
class T
{
public:
T()
{
OutputDebugString("i was called, to do some work!\n");
}
};
int main()
{
T(); // output ctor-text
return 0;
}
Output:
i was called, to do some work
But there are very rare cases, where this makes sense
Mikey
-
December 1st, 2002, 12:18 PM
#9
Mikey :
Thanks very much 
If you want to do it like what you mentioned , why don't use a stactic function or a globle function ? A instantiation of object is very costly.
I just want to know the roof cause
-
December 1st, 2002, 12:26 PM
#10
Originally posted by flysnow
If you want to do it like what you mentioned , why don't use a stactic function or a globle function ? A instantiation of object is very costly.
Yes, of course a static function or whatever would be better.
As I said, there are very rare cases for the need of an object without var. (And currently I can't imaging one )
It was just an example, how You can use it.
Mikey
-
December 1st, 2002, 12:35 PM
#11
A useful case would be to return it from a function:
Code:
CString getString(LPCSTR sz)
{
// create 2 temp. objects (return the first one)
return CString(CString(sz)/*temp. obj to use the '+'-operator*/ + "hello");
}
CString cs = getString("hi and "); // cs="hi and hello"
Mikey
Last edited by Mikey; December 1st, 2002 at 12:38 PM.
-
December 1st, 2002, 12:38 PM
#12
Mikey :
Do you know who is the most famous expert in this C++ board ? I think only the C++ expert can answer this questin .
-
December 1st, 2002, 12:42 PM
#13
I think Your question was already answered some mails ago...
Mikey
-
December 1st, 2002, 12:51 PM
#14
Originally posted by Mikey
A useful case would be to return it from a function:
Code:
CString getString(LPCSTR sz)
{
// create 2 temp. objects (return the first one)
return CString(CString(sz)/*temp. obj to use the '+'-operator*/ + "hello");
}
CString cs = getString("hi and "); // cs="hi and hello"
Mikey
But I think it isn't the exact explanation for my question , If you traced the assemble code of my sampe code in VC , you will find that a destuctor function was called immediately after construtor function was called on the temporary object .
So I don't think it can return a object by that way .
-
December 1st, 2002, 01:09 PM
#15
Since You don't save Your object in a var, it's destroyed after use.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|