0

In C, you can dynamically link a shared object by dlopen() and use dlsym() to locate the address of a particular symbol( a function ) and assign it to a function pointer.

In C++, if you happen to know the address of the class constructor, will you be able to create a new object out of it ? I do realize that that, using a static method which would return a object of the class is the convention for dynamically linking in c++.

I was inquisitive whether can create an object from a constructors address. If so, can u give an example ?

Thanks in advance

KodeWarrior
  • 3,538
  • 3
  • 26
  • 40

1 Answers1

1

You can't. The problem is that you can't call the constructor directly; all constructor calls are implicitly generated by the compiler. This also means you can't call it through a function pointer.

I also just tried to take the address of a constructor, which didn't work either (although that was just a quick hack, can't be sure it's actually valid) -- as constructors don't have a return type, the next question would have been how to write the type for that function pointer.

Christian Stieber
  • 9,954
  • 24
  • 23