Mimicing C # 'new' (hiding a virtual method) in a C ++ code generator

I am developing a system that accepts a set of compiled .NET collections and emits C ++ code, which can then be compiled to any platform that has a C ++ compiler. Of course, this is due to some extensive cheats due to various things that .NET does with C ++.

One of these situations is the ability to hide virtual methods, for example, the following in C #:

class A
{
    virtual void MyMethod()
    { ... }
}

class B : A
{
    override void MyMethod()
    { ... }
}

class C : B
{
    new virtual void MyMethod()
    { ... }
}

class D : C
{
    override void MyMethod()
    { ... }
}

I came up with a solution for this that seemed smart and really worked, as in the following example:

namespace impdetails
{
template<class by_type>
struct redef {};
}

struct A
{
    virtual void MyMethod( void );
};

struct B : A
{
    virtual void MyMethod( void );
};

struct C : B
{
    virtual void MyMethod( impdetails::redef<C> );
};

struct D : C
{
    virtual void MyMethod( impdetails::redef<D> );
};

This of course requires that all call sites for C::MyMethodand D::MyMethodbuild and pass a dummy object, as in this example:

C *c_d = &d;
c_d->MyMethod( impdetails::redef<C>() );

; .

, , . , , , impdetails::redef<> , , .

++ , , , , . , .

, , VC2008, , , ! , , .

, , , , MyMethod, MyMethod$1 MyMethod$2. . , $ ++ ( , ). , , .

, , , , impdetails::redef<>.

- , ?

+4
1

, .NET, , , - , - ++ . , , , .

vtable (, , vtabletype::). , , . - vtable.

, , . VC2008. ( , C, , , , this .)

, , , . :

  • , , , , newslot, .

  • ( ), ++ , .

  • .NET , .ctor. . vtables . ( .ctor - - .)

  • .

  • , .

++ vtable: , , . ( _0, _1...) , , , , - .

, , , , , , , , ( .)

0

All Articles