Hi all!

I'm trying to call the constructor of a C++ class in Visual C++'s inline assembly. It tells me either that it can't take the address of a constructor (why not? I have a valid pointer in ecx), or it can't match it with an existing declaration, or it's an improper operand type.

Actually, I just need the pointer to the constructor. That's all. :sweat:

Thanks for any ideas!
Posted on 2004-04-07 06:32:46 by C0D1F1ED
assembly listing helps in this case ...
Posted on 2004-04-07 15:42:05 by Ultrano
I'd like an automatic process (i.e. letting the Linker resolve it). Else I would have to check the listing every compilation... :notsure:
Posted on 2004-04-07 20:02:14 by C0D1F1ED
You always have at least two constructors for any class containing member functions - the parameterless "default" constructor, and the "copy" constructor taking one parameter. I don't recall any way to disambiguate overloaded functions with an address operator (&). So it looks like it can't be done in C++.

Constructors are also not virtual, so you won't be able to chase it down via the object pointer.

So it looks like you will need the compiler to tell you what the constructor's "link" name is.
Posted on 2004-04-07 23:58:54 by tenkey

You always have at least two constructors for any class containing member functions - the parameterless "default" constructor, and the "copy" constructor taking one parameter. I don't recall any way to disambiguate overloaded functions with an address operator (&). So it looks like it can't be done in C++.

Thanks, that's a logical explantion!

I sortof solved the problem this way:


class MyClass
{
static MyClass *create()
{
return new MyClass;
}

MyClass();

// ...
};

It's not entirely equivalent, but it serves my purpose...
Posted on 2004-04-08 09:28:37 by C0D1F1ED
You could use 'placement new' to call the constructor.



#include <new>

class MyObj
{
...
};

void foo()
{
char buf[sizeof(MyObj)];
// call MyObj constructor
new (buf) MyObj;
...
// call destructor
reinterpret_cast<MyObj &>(buf).~MyObj();
}
Posted on 2004-04-15 15:32:35 by death