Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pure virtual functions and binary compatibility

Now, I know it is generally bad to add new virtual functions to non-leaf classes as it breaks binary compatibility for any derived classes which haven't been recompiled. However, I have a slightly different situation:

I have an interface class and implementation class compiled into a shared library, for example:

class Interface {
    public:
        static Interface* giveMeImplPtr();
        ...
        virtual void Foo( uint16_t arg ) = 0;
        ...
}

class Impl {
    public:
        ...
        void Foo( uint16_t arg );
        ....
}

My main application uses this shared library, and could basically be written as:

Interface* foo = Implementation::giveMeImplPtr();
foo->Foo( 0xff );

In other words, the application doesn't have any classes which derive from Interface, it merely uses it.

Now, say I want to overload Foo( uint16_t arg ) with Foo( uint32_t arg ), am I safe to do:

 class Interface {
    public:
        static Interface* giveMeImplPtr();
        ...
        virtual void Foo( uint16_t arg ) = 0;
        virtual void Foo( uint32_t arg ) = 0;
        ...
}

and recompile my shared library without having to recompile the application?

If so, are there any unusual caveats I need to be aware of? If not, do I have any other options other than to take the hit and up-version the library, thus breaking backwards compatibility?

like image 304
Matt Dunn Avatar asked Feb 14 '13 12:02

Matt Dunn


People also ask

What is binary compatibility C++?

Definition. A library is binary compatible, if a program linked dynamically to a former version of the library continues running with newer versions of the library without the need to recompile.

When a function should be make pure virtual?

A pure virtual function (or abstract function) in C++ is a virtual function for which we don't have an implementation, we only declare it. A pure virtual function is declared by assigning 0 in the declaration. These are the concepts of Run-time polymorphism.

What is the difference between virtual functions and pure virtual functions?

A virtual function is a member function in a base class that can be redefined in a derived class. A pure virtual function is a member function in a base class whose declaration is provided in a base class and implemented in a derived class. The classes which are containing virtual functions are not abstract classes.

What are the properties of pure virtual function?

A pure virtual function is a function that must be overridden in a derived class and need not be defined. A virtual function is declared to be “pure” using the curious =0 syntax.


1 Answers

The simple answer is: no. Anytime you change the class definition at all, you potentially lose binary compatibility. Adding a non-virtual function or static members is usually safe in practice, although still formally undefined behavior, but that's about it. Anything else will probably break binary compatibility.

like image 56
James Kanze Avatar answered Oct 06 '22 09:10

James Kanze