Pergunta

I have a library that can be compiled as a shared library (or DLL in Windows). It has a class that is derived from another class in another library. The base class has some virtual methods and my class overrides a few of them. For example:

class Base {
public:
    virtual void method1();
    virtual void method2();
    virtual void method3();
};

class Derived: public Base {
public:
    virtual void method2();
};

Now I figured out that one of the virtual methods doesn't quite work for my class. Currently it doesn't override this method, so I want to override it too to fix its behavior:

class Derived: public Base {
public:
    virtual void method2();
    virtual void method3();
};

Will this break binary compatibility with older versions of my library?

As far as I understand it, it is different from just adding virtual functions, as the number and the order of virtual methods in the vtable stays the same. The only difference is that a particular entry in the vtable for my class will now contain a different value. Is this correct?

I'm also quite sure that none of the applications currently using my library use that method, since it is completely broken and never works. So I'm not worrying about breaking existing calls to the base method implementation. I just want to make sure that I won't break anything else.

Foi útil?

Solução

Since you're talking about DLLs, I assume this is C++ in Visual Studio/Windows. Adding the override will not break binary compatibility because the size of the vtable is not changing. However, it may cause some undesired results if you don't recompile all code that instantiates new instances of Derived. This is because the vtable is initialized by the instantiating source, not the source of the class that implements Derived.

Outras dicas

If I understood correctly you have your Base class in Dll1 and your Derived class in Dll2. If this is the case the change you describe doesn't affect Dll1. Assuming you'll install an updated Dll2 your application will switch to call Derived::method3() whenever you access instances of Derived through pointers or references to Base.

The library your derived class (that added the new virtual method) exists in will not necessarily be binary (ABI) compatible with the old version of the library. This is because you have no control over how the vtable is generated by the compiler when you add the overridden virtual method.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top