Author Topic: Calling constructor in Visual C++ inline assembly  (Read 1104 times)

0 Members and 1 Guest are viewing this topic.

Offline C0D1F1ED

  • Code Warrior
  • **
  • Posts: 215
    • http://softwire.sourceforge.net
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!

Offline Ultrano

  • Community Staff
  • ASM Fanatic
  • *****
  • Posts: 1612
  • Country: gb
Calling constructor in Visual C++ inline assembly
« Reply #1 on: 2004-04-07 20:42:05 »
assembly listing helps in this case ...
Knowledge is Power. Power Corrupts. Study Hard. Be Evil

Offline C0D1F1ED

  • Code Warrior
  • **
  • Posts: 215
    • http://softwire.sourceforge.net
Calling constructor in Visual C++ inline assembly
« Reply #2 on: 2004-04-08 01:02:14 »
I'd like an automatic process (i.e. letting the Linker resolve it). Else I would have to check the listing every compilation... :notsure:

Offline tenkey

  • Regular Member
  • ***
  • Posts: 616
Calling constructor in Visual C++ inline assembly
« Reply #3 on: 2004-04-08 04:58:54 »
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.

Offline C0D1F1ED

  • Code Warrior
  • **
  • Posts: 215
    • http://softwire.sourceforge.net
Calling constructor in Visual C++ inline assembly
« Reply #4 on: 2004-04-08 14:28:37 »
Quote from: 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:
Code: [Select]

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

    MyClass();

    // ...
};

It's not entirely equivalent, but it serves my purpose...

Offline death

  • Code Warrior
  • **
  • Posts: 133
Calling constructor in Visual C++ inline assembly
« Reply #5 on: 2004-04-15 20:32:35 »
You could use 'placement new' to call the constructor.

Code: [Select]

#include <new>

class MyObj
{
...
};

void foo()
{
    char buf[sizeof(MyObj)];
    // call MyObj constructor
    new (buf) MyObj;
    ...
    // call destructor
    reinterpret_cast<MyObj &>(buf).~MyObj();
}
DEATH is a part of life