Page 1 of 1
Explicitly calling a constructor in C++
Posted: Sat Jun 25, 2011 5:33 am
by Neolander
This is something which I've been struggling with for a long while. The last hack which I used to escape this problem has just blown up, and I'm tired of running away.
How does one explicitly call the constructor of an object in C++ ? Making the object.Class() call is apparently forbidden by the C++ standard (according to g++'s output when I try to do it at least), but without that I don't know how one is supposed to initialize an object which has just been allocated on a heap and is filled with random property values. The STL's default new operator, at the very least, must have a way to do it. Question is, what is this way ?
Re: Explicitly calling a constructor in C++
Posted: Sat Jun 25, 2011 6:20 am
by Solar
Neolander wrote:How does one explicitly call the constructor of an object in C++?
Code: Select all
// Variant 1 - Stack
MyClass foo( 42 );
// Variant 2 - Heap
MyClass * foo = new MyClass( 42 );
// Variant 3 - Heap (placed)
char memory[ sizeof( MyClass ) ];
void * memptr = memory;
MyClass * foo = new( memptr ) MyClass( 42 );
Above list is complete, i.e. (AFAIK) there are no other ways to call a constructor.
I don't know how one is supposed to initialize an object which has just been allocated on a heap and is filled with random property values.
I'm not quite sure I understood correctly.
If you want to
initialize an object at a given location, i.e. writing new values into that location, see variant 3 above.
If you want to
use whatever is at a given location
as if it were a properly initialized, what you're looking for is this:
Code: Select all
void * somePtr;
// ...which points to some memory region you want to use as if
// it were a MyClass object
MyClass * foo = reinterpret_cast< MyClass >( somePtr );
foo->whateverFunction();
Note that the use of
reinterpret_cast is considered a "code smell" by some (i.e., will receive additional scrutiny as it's considered bad coding practice).
Re: Explicitly calling a constructor in C++
Posted: Sat Jun 25, 2011 7:23 am
by Neolander
What I want to do is to initialize an object at a given location, so variant 3. But does placed new exist in a freestanding environment, without library support ? And if not, how could I implement it ?
Re: Explicitly calling a constructor in C++
Posted: Sat Jun 25, 2011 8:23 am
by Neolander
And the compiler will do the rest of the initialization job ?
Re: Explicitly calling a constructor in C++
Posted: Sat Jun 25, 2011 11:27 am
by TylerH
Neolander wrote:And the compiler will do the rest of the initialization job ?
Yes. It makes since is you remember that
Code: Select all
MyClass instance_of_myclass(parameters);
is implicitly
Code: Select all
MyClass instance_of_myclass;
instance_of_myclass.MyClass(parameters);
Re: Explicitly calling a constructor in C++
Posted: Sat Jun 25, 2011 11:49 am
by Neolander
Sure, but I wondered about placement new... Oh, well, guess it works the same way...
EDIT : ...because if it does not, I'll *very* quickly find out.
Re: Explicitly calling a constructor in C++
Posted: Sun Jun 26, 2011 2:47 am
by Neolander
This I know, I just have a hard time separating what part of new is a core language feature and what part of it requires a library implementation
Re: Explicitly calling a constructor in C++
Posted: Sun Jun 26, 2011 3:00 am
by Neolander
Guess I must be unlucky, because everything I found focused on implementation details without explaining that kind of useful theory first. Which tutorials do you use ?
Re: Explicitly calling a constructor in C++
Posted: Sun Jun 26, 2011 3:12 am
by Neolander
Thanks !
EDIT : (I now accept your "read" criticism, considering that I have managed to read this very article without noticing the relevant part. Shame on me)
Re: Explicitly calling a constructor in C++
Posted: Sun Jun 26, 2011 3:20 am
by Neolander
Silly question : for the "normal" new operator, is there a way to have the compiler check that the returned pointer is not NULL before trying to initialize it ? I don't have exception support at the kernel level, and I think it'd be very tricky to implement to be honest.
EDIT : If the std::nothrow variant of standard new does it, there has to be a way to do it without the STL's new.h, right ?
Re: Explicitly calling a constructor in C++
Posted: Sun Jun 26, 2011 3:34 am
by Neolander
Thanks again ! Should try to read this standard cover to cover once, though it'd probably be one of the most headache-inducing things I'd have ever done in my life.