| View previous topic :: View next topic |
| Author |
Message |
shsingh Guest
|
Posted: Wed Oct 18, 2006 7:57 am Post subject: How to call a constructor explicitly if we want to allocate |
|
|
I have a class A containing some map as data variables. I creat an
object of class A on heap by allocatiing memory by using "malloc". This
will return me the required memory but the object is not initialized
properly as constructor same is not get called ( as per the behavior).
How to call a constructor explicitly if we want to allocate memory
using malloc ? |
|
| Back to top |
|
 |
Larry Smith Guest
|
Posted: Wed Oct 18, 2006 8:11 am Post subject: Re: How to call a constructor explicitly if we want to alloc |
|
|
shsingh wrote:
| Quote: | I have a class A containing some map as data variables. I creat an
object of class A on heap by allocatiing memory by using "malloc". This
will return me the required memory but the object is not initialized
properly as constructor same is not get called ( as per the behavior).
How to call a constructor explicitly if we want to allocate memory
using malloc ?
|
Your create object on the heap with new, not malloc.
A *pA = new A;
You free the memory and object with delete.
delete pA; |
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Oct 18, 2006 8:46 am Post subject: Re: How to call a constructor explicitly if we want to alloc |
|
|
shsingh wrote:
| Quote: | I have a class A containing some map as data variables. I creat an
object of class A on heap by allocatiing memory by using "malloc".
This will return me the required memory but the object is not
initialized properly as constructor same is not get called ( as per
the behavior).
How to call a constructor explicitly if we want to allocate memory
using malloc ?
|
Use "placement new" (look it up).
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask |
|
| Back to top |
|
 |
Bart Guest
|
Posted: Wed Oct 18, 2006 9:10 am Post subject: Re: How to call a constructor explicitly if we want to alloc |
|
|
shsingh wrote:
| Quote: | I have a class A containing some map as data variables. I creat an
object of class A on heap by allocatiing memory by using "malloc". This
will return me the required memory but the object is not initialized
properly as constructor same is not get called ( as per the behavior).
How to call a constructor explicitly if we want to allocate memory
using malloc ?
|
You can use placement new.
buffer = malloc(/*...*/);
object = new (buffer) MyClass(/*...*/);
But later you have to call the destructor explicitly too:
object->~MyClass();
free(buffer);
Regards,
Bart. |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sat Oct 21, 2006 9:10 am Post subject: Re: How to call a constructor explicitly if we want to alloc |
|
|
* Kai-Uwe Bux:
[about "can't call constructor" urban myth]
I recently ran into yet another formulation in the standard that uses
the "call" terminology, namely §1.9/15, about an initializer like
Foo object( arg1, arg2, arg3 ... );
"the resulting construct is a function call upon a constructor function,
with expression-list as an argument list; such a function call is a
full-expression".
Can't be very much more clear than that, I think. ;-)
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? |
|
| Back to top |
|
 |
Guest
|
Posted: Sat Oct 21, 2006 9:10 am Post subject: Re: How to call a constructor explicitly if we want to alloc |
|
|
| Quote: | How to call a constructor explicitly if we want to allocate
memory using malloc ?
|
This thread has been an excellent read for me. "placement new" is a
concept I had not heard of before. Since I do some kernel level
development it is an excellent language feature because you often are
given context buffer space that you overlay a structure onto. Without
placement new, there ar two suboptimal solutions: either avoid
constructors or put a pointer to the object (instead of the object
itself) in the context buffer and new/delete it.
If there are any other concepts that might especially be useful in
kernel level development, please mention. |
|
| Back to top |
|
 |
|