Explicitly calling a constructor in C++
Explicitly calling a constructor in C++
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 ?
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++
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 );
I'm not quite sure I understood correctly.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.
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();
Every good solution is obvious once you've found it.
Re: Explicitly calling a constructor in C++
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++
And the compiler will do the rest of the initialization job ?
Re: Explicitly calling a constructor in C++
Yes. It makes since is you remember thatNeolander wrote:And the compiler will do the rest of the initialization job ?
Code: Select all
MyClass instance_of_myclass(parameters);
Code: Select all
MyClass instance_of_myclass;
instance_of_myclass.MyClass(parameters);
Re: Explicitly calling a constructor in C++
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.
EDIT : ...because if it does not, I'll *very* quickly find out.
Re: Explicitly calling a constructor in C++
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++
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++
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)
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++
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 ?
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.
Re: Explicitly calling a constructor in C++
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.