Reputation: 805
Is it possible to pass some parameters to constructor of a class inside a constructor of another class using malloc
? I can do that with new
. I need to do the same thing with malloc
:
(If it does not make sense, consider that I am using a custom allocator instead of malloc)
Class A_Class ... {
public:
B_Class *b;
...
A_Class: ...
{ b = new B_Class (c1, c2, c3);
} // end of constructor
}
Now with malloc:
Class A_Class ... {
public:
B_Class *b;
...
A_Class: ...
{ b = (B_Class*) malloc (sizeof(*b));
??????????????? }
}
Upvotes: 2
Views: 4048
Reputation: 320431
malloc
allocates raw memory. There's no point in trying to pass constructor arguments to it because it doesn't call any constructors.
If you have to work with raw memory, it is up to you to construct an object in previously allocated raw memory by using "placement new" syntax
...
void *raw_b = malloc(sizeof *b);
b = new(raw_b) B_Class(c1, c2, c3); // <- placement new
...
Numerically, the value of b
will be the same as raw_b
, i.e. it is possible to get by without an extra raw_b
pointer. But I prefer to do it this way, with an intermediate void *
pointer, to avoid ugly casts.
Be careful when destroying such objects, of course. Custom allocation requires custom deletion. You can't just delete
your b
in general case. A symmetrical custom deletion sequence would involve an explicit destructor call followed by raw memory deallocation
b->~B_Class(); // <- explicit destructor call
free(b);
P.S. You might consider going a different route: overloading operator new
/operator delete
in your class instead of spelling out custom allocation explicitly every time you need to create an object.
Upvotes: 13
Reputation: 279255
No, it's not possible. If it is designed for C++, then your custom allocator will have a function called construct
that is used for this (and rebind
to get an allocator that can construct a different type).
In C++03, this only does copy-construction, so you would have to call allocator.construct(ptr, B_Class(c1, c2, c3))
and the class has to be copyable. In C++11, allocators can construct from any arguments.
If your allocator is designed for C, then you'll have to use so-called "placement new". Of course, malloc
is designed for C. You should look it up for yourself, but the syntax is:
b = (B_Class*) malloc(sizeof(*b));
new ((void*)b) B_Class(c1,c2,c3);
Remember to handle any exception that the constructor may throw, and free
the buffer.
Upvotes: 2
Reputation: 5411
You can create static method for constructing B:
class B_Class
{
public static B_Class* MallocMe(SomeParam param)
{
B_Class* retval = (B_Class*) malloc(sizeof(B_Class));
retval->Construct(param);
return retval;
}
....
Upvotes: 0
Reputation: 153909
malloc
doesn't call constructors. There's no way to pass parameters to a function which doesn't get called. As for the custom allocator, you'll have to be more specific as to what you're doing. Who's allocating what where using the custom allocator. Usually, custom allocators are only used for allocation; they aren't concerned with the constructor either.
Upvotes: 0