mono / CppSharp

Tools and libraries to glue C/C++ APIs to high-level languages
MIT License
3.14k stars 518 forks source link

Incorrect handling of virtual methods #402

Closed tritao closed 9 years ago

tritao commented 9 years ago

Consider this case:

C++: static Base getBase() { return Derived(); }

C#: Base b = Base.getBase(); b.virtual();

Since we're not generating code to go through the virtual table in Base we'll not have the expected polymorphic behaviour.

genuinelucifer commented 9 years ago

@tritao I tried to reproduce it but could not because probably I didn't quite understand it. Please explain this a bit...

ddobrev commented 9 years ago

@genuinelucifer imagine that in the Base of the example you have a virtual function defined as follows:

int Base::virtualFunction()
{
    return 1;
}

Then in the Derived from the example you override this function:

int Derived::virtualFunction()
{
    return 2;
}

At present, when you call b.virtualFunction() - as in the definition of the issue - you'll get 1. However, this is wrong - you should get 2 because the real C++ type that is returned is Derived.

ddobrev commented 9 years ago

@genuinelucifer You need to use the same code we use for calls in abstract implementations:

https://github.com/mono/CppSharp/blob/master/src/Generator/Generators/CSharp/CSharpTextTemplate.cs#L2202

genuinelucifer commented 9 years ago

@ddobrev Thanks. Will try to implement it.

genuinelucifer commented 9 years ago

@ddobrev Please see #496. I have tried but the test is failing at one instance. It feels a bit weird to me.