I have an abstract class in my DLL.
class Base {
virtual char * First() = 0;
virtual char * Second() = 0;
virtual char * Third() = 0;
};
This dinamic library and this interface are used for a long time. There is my mistake in my code. Now I want to change this interface
class Base {
virtual const char * First() const = 0;
virtual const ch开发者_运维问答ar * Second() = 0;
virtual char * Third() const = 0;
};
Some EXE-program uses my DLL. Will the EXE-program work without recompilation? Consider changes in each line of new interface independently.
Note: of course, EXE-program does not change functions results.
Your EXE could change result of function since it was char*
. Now it is const char*
. And changing const object will lead to undefined behavior according to C++ Standard 7.1.5.1/3-4:
A pointer or reference to a cv-qualified type need not actually point or refer to a cv-qualified object, but it is treated as if it does; a const-qualified access path cannot be used to modify an object even if the object referenced is a non-const object and can be modified through some other access path. [Note: cv-qualifiers are supported by the type system so that they cannot be subverted without casting (5.2.11). ]
Except that any class member declared mutable (7.1.1) can be modified, any attempt to modify a const object during its lifetime (3.8) results in undefined behavior.
It "shouldn't" work, but you never know your luck.
Because of overloading, char *First()
and const char *First() const
are different functions. You could have both in the same class. So any name-mangling scheme has to map them to different names, which obviously is a problem when it comes to binding.
But, these are virtual calls, and you have three functions replaced by their equivalents in the same order. I don't know any details of MSVC's vtable scheme, in particular whether the offsets are statically determined or dynamically bound. If the former, it's possible that the exe can bind against the new vtable. The function pointers might just so happen to work, because the calling convention doesn't depend on cv-qualification (that is, a const char*
is returned the same way a char*
is, and const this is passed the same way non-const this is).
Even if it does work, I wouldn't want to rely on it unless it's something that MS specifically addresses and guarantees.
since you change your interface, you have to recompile (i think)
Probably won't work. Though the easiest way to know for sure is to try it and see
I don't think this will work. Changing the interface of a DLL usually requires the executable that links to it to be recompiled.
In addition, you will likely need to make changes to the code in the executable, as you have changed the function signatures.
Lastly, if you are going to update/append an interface, you would be best to subclass the original interface. This will prevent any existing code from breaking.
精彩评论