Sign in to follow this  
Yohomyth

linking virtual members

Recommended Posts

I'm not sure on how virtual members work, but could you do something like this?:
//MyDll.cpp

class __declspec(dllexport) CMyClass
{
public:
    virtual void DoSomething() { }
};

__declspec(dllexport) void MakeCall(CMyClass *o)
{
    o->DoSomething();
}

//MyExe.cpp

class __declspec(dllimport) CMyClass
{
public:
    virtual void DoSomething();
};

__declspec(dllimport) void MakeCall(CMyClass *o);

class CMySubClass : public CMyClass
{
public:
    virtual void DoSomething();
} *MyObj;

MakeCall((CMyClass*)MyObj);
I exported a class from a DLL, made a subtype of this class in the EXE, and made a call to a function in the DLL that makes a call to a virtual member function of the origonal class. Will it make a call to the CMySubClass's version of the function? BTW: Sorry if I'm unclear (I usually am).

Share this post


Link to post
Share on other sites
Yes, I do this all the time. Be careful of the fragile base class problem; that is, whether the base class has changed between the time the EXE was built and the DLL was linked. It is possible to link the two when one is using an old definition, and the vtable layout won't match up.

If this turns out to be a problem I think you can safely add virtual member functions to the end of the base class definition and DLLs using older versions of the base class should still call the correct ones. Since the vtable is laid out sequentially, this makes sense. However none of this is guaranteed by Ye Holy Standard, it is more a by-product of common implementation details.

And the base cast is doesn't need to be explicitly specified.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this