1

Here’s an example:

struct I
{
    virtual void method() = 0;
    virtual void method( const char* something ) = 0;
};

class C : public I
{
    void method() override
    {
        printf( "void" );
    }
    void method( const char* something ) override
    {
        printf( "string" );
    }
};

The vtable order is flipped in the I, the method with the string argument is before the method without arguments.

This only happens for methods with the same name.

Is there a compiler switch to prevent the compiler from reordering my methods, even if they have the same name?

Soonts
  • 20,079
  • 9
  • 57
  • 130
  • The answer is most probably "No!" See [Virtual Table layout in memory?](https://stackoverflow.com/q/1342126/10871073) for a discussion (especially the [answer](https://stackoverflow.com/a/1342148/10871073) by Naveen). – Adrian Mole Apr 04 '20 at 03:14
  • @AdrianMole If compilers were randomly reordering them, COM wouldn’t be possible, yet people use COM for decades now. Here’s more context: https://github.com/Const-me/ComLightInterop The issue only happens on Windows with VC++, and only for methods with the same name. – Soonts Apr 04 '20 at 03:25
  • Compilers aren't randomly reordering them. They are systematically ordering them according to criteria that are specific to each compiler (or to the ABI being used, which may be used by multiple compilers). Otherwise, code compiled with one compiler could not consistently link, and COM already would not work. – Peter Apr 04 '20 at 04:03

0 Answers0