简体   繁体   中英

C++ Virtual Function not enforced

I have an interface class (see first code block), the implementation (see second code block) doesn't generate an error, even though CreateMessage() isn't defined... I am totally confused because as far as I see this shouldn't have compiled.

Oh, using Visual Studio 2010 on Microsoft 7... anyone seen this before or have I got a bug in my code?

Code Block 1:

namespace Common
{

namespace Messaging
{

class iNetworkMessageFactory
{
public:

    //  +----------------------------------------------------------------------
    //  + Description : iNetworkMessageFactory class destructor.
    //  +----------------------------------------------------------------------
    virtual ~iNetworkMessageFactory() { }


    //  +----------------------------------------------------------------------
    //  + Description : Create a network message.
    //  +----------------------------------------------------------------------
    virtual iNetworkMessage *CreateMessage() = 0;
};

}   // Namespace Common

}   // Namespace Messaging

Code Block 2:

#include <Messaging/iNetworkMessageFactory.h>


class NetworkMessageFactory : public Common::Messaging::iNetworkMessageFactory
{
public:
    //  +----------------------------------------------------------------------
    //  + Description : NetworkMessageFactory class constructor.
    //  +----------------------------------------------------------------------
    NetworkMessageFactory();


    //  +----------------------------------------------------------------------
    //  + Description : NetworkMessageFactory class destructor.
    //  +----------------------------------------------------------------------
    ~NetworkMessageFactory();

    /*
    //  +----------------------------------------------------------------------
    //  + Description : Create a network message.
    //  +----------------------------------------------------------------------
    iNetworkMessage *CreateMessage( NetworkMessageType typeID
                                  , iNetMsgBody *body
                                  , ConnectionID connID );
    */
};

That's fine, and should not generate an error. Otherwise, you would not be able to have more than one abstract ancestor. You'll get a runtime error if you attempt to call the pure virtual.

For example, C++ would allow you to subclass NetworkMessageFactory and provide an implementation of the pure virtual function in the derived class. There is no reason to enforce the existence of an implementation in the NetworkMessageFactory class itself.

This is allowed, but you will not be able to instantiate the class NetworkMessageFactory because the pure virtual methods CreateMessage hasn't been defined.

Consider the following case: you have a pure virtual base class 'A', which has two pure virtual methods: do_work, and do_more_work. Let's say that you have two more classes, B and C, which have the same implementation of 'do_work', but a different implementation of 'do_more_work'. you then have a class called 'D', which has the same implementation of 'do_more work' as C, but a different 'do_work', implementation.

Convoluted? Yes. But in this hypothetical situation, you could create intermediate classes which define the shared implementations of the virtual methods and then have A, B, C and D inherit those definition from them. These intermediate classes won't necessarily have a definition for both 'do_work', and 'do_more_work', so C++ allows you to define these intermediate classes for use in your class hierarchy, but not instantiate them on their own.

The example I've described improves code reuse (albeit at a cost), and ideally, makes your code easier to maintain (this is contentious!). Before you begin writing code like this, take a good hard look at your problem and try and find a simpler solution; this sort of thing could give you and your colleagues a serious headache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM