Explicitly calling a constructor in C++

Programming, for all ages and all languages.
Post Reply
User avatar
Neolander
Member
Member
Posts: 228
Joined: Tue Mar 23, 2010 3:01 pm
Location: Uppsala, Sweden
Contact:

Explicitly calling a constructor in C++

Post 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 ?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Explicitly calling a constructor in C++

Post 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).
Every good solution is obvious once you've found it.
User avatar
Neolander
Member
Member
Posts: 228
Joined: Tue Mar 23, 2010 3:01 pm
Location: Uppsala, Sweden
Contact:

Re: Explicitly calling a constructor in C++

Post 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 ?
User avatar
Neolander
Member
Member
Posts: 228
Joined: Tue Mar 23, 2010 3:01 pm
Location: Uppsala, Sweden
Contact:

Re: Explicitly calling a constructor in C++

Post by Neolander »

And the compiler will do the rest of the initialization job ?
TylerH
Member
Member
Posts: 285
Joined: Tue Apr 13, 2010 8:00 pm
Contact:

Re: Explicitly calling a constructor in C++

Post 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);
User avatar
Neolander
Member
Member
Posts: 228
Joined: Tue Mar 23, 2010 3:01 pm
Location: Uppsala, Sweden
Contact:

Re: Explicitly calling a constructor in C++

Post 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.
User avatar
Neolander
Member
Member
Posts: 228
Joined: Tue Mar 23, 2010 3:01 pm
Location: Uppsala, Sweden
Contact:

Re: Explicitly calling a constructor in C++

Post 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 :)
User avatar
Neolander
Member
Member
Posts: 228
Joined: Tue Mar 23, 2010 3:01 pm
Location: Uppsala, Sweden
Contact:

Re: Explicitly calling a constructor in C++

Post 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 ?
User avatar
Neolander
Member
Member
Posts: 228
Joined: Tue Mar 23, 2010 3:01 pm
Location: Uppsala, Sweden
Contact:

Re: Explicitly calling a constructor in C++

Post 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)
User avatar
Neolander
Member
Member
Posts: 228
Joined: Tue Mar 23, 2010 3:01 pm
Location: Uppsala, Sweden
Contact:

Re: Explicitly calling a constructor in C++

Post 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 ?
Last edited by Neolander on Sun Jun 26, 2011 3:28 am, edited 1 time in total.
User avatar
Neolander
Member
Member
Posts: 228
Joined: Tue Mar 23, 2010 3:01 pm
Location: Uppsala, Sweden
Contact:

Re: Explicitly calling a constructor in C++

Post 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.
Post Reply