Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a function pointer to the a function's `new` operator/constructor?

If I were to wanted to parameterize creating an object, I could of course make a function which called new on a particular class and passed out a pointer. I am wondering if it's possible to skip that step and pass a function pointer to the new operator itself.

like image 675
Catskul Avatar asked Jul 07 '11 01:07

Catskul


People also ask

Can we have a pointer to a constructor?

Some people feel you should not use the this pointer in a constructor because the object is not fully formed yet. However you can use this in the constructor (in the {body} and even in the initialization list) if you are careful.

Can we call member function using this pointer from constructor in C++?

the constructor is the first function which get called. and we can access the this pointer via constructor for the first time. if we are able to get the this pointer before constructor call (may be via malloc which will not call constructor at all), we can call member function even before constructor call.

Which operator is used in pointer to member function?

The . * operator is used to dereference pointers to class members. The first operand must be of class type.


1 Answers

boost::lambda provides function wrappers for new and delete. These can be used to easily convert an new call into a function object.

like image 50
bdonlan Avatar answered Sep 28 '22 16:09

bdonlan