What's the difference between virtual function instantiations in C++?

后端 未结 8 1894
栀梦
栀梦 2021-01-25 07:30

What\'s the difference between the following two declarations?

virtual void calculateBase() = 0;  
virtual void calculateBase();

I read the fir

相关标签:
8条回答
  • 2021-01-25 07:39

    The first one doesn't have to be implemented in the base class, but enforces it to be implemented in inherited classes.

    You have to implement the second one in the base class and it can be implemented in inherited classes.

    0 讨论(0)
  • 2021-01-25 07:48

    I think you are mixing terms...

    virtual void x() = 0;
    

    is a pure virtual function, or abstract function. It is a virtual function without implementation. You talk about a pure abstract class, which is the c++ equivalent of an interface, about a class having only abstract functions.

    virtual void x();
    

    is a virtual function, meaning it can be redefined in derived classes, but it's not abstract, so you must provide an implementation for it.

    0 讨论(0)
  • 2021-01-25 07:51

    virtual void calculateBase() = 0;

    The first function is Pure virtual function where the implementation can not be done in the class and it act as pure abstract class or Interface class. The concrete implementation should be done or overriden in subclass. It act as interface class to its derived classes.

    virtual void calculateBase();

    This function is virtual function where the implementation (default) can be done in the base class. But, the derived class must be overriden its own imeplementation.

    0 讨论(0)
  • 2021-01-25 07:56

    The first one is a "pure virtual" - it will make the class abstract, attempting to instantiate it will result in compiler errors. It is meant to be used as a base class where the derived class implements the necessary behaviour by implementing the pure virtual function. You do not have to implement the function in the base class, though you can.
    This is a pattern often used for two design patterns:

    • The "template method" design pattern, where the base class implements a structure around a function call, but the details of the function call must be filled in by the derived class, and
    • The "interface" design pattern, because C++ doesn't have the interface keyword. Abstract base classes, ideally with only pure virtual functions and no member data, are the C++ way of defining interfaces.

    The second declaration is just an ordinary virtual member function declaration. You will get compiler errors if you fail to implement the member function in the base class. It is still virtual which implies that it may be useful to override the behaviour in a derived class.

    0 讨论(0)
  • 2021-01-25 07:56

    The second function must have an implementation in the class that declare it (lack of '= 0'), and can be overriden by subClasses.

    The first function may or not have an implementation in the class that declare it, and has to be implemented by subClasses

    0 讨论(0)
  • 2021-01-25 07:57

    First one is called a pure virtual function. Normally pure virtual functions will not have any implementation and you can not create a instance of a class containing a pure virtual function.

    Second one is a virtual function (i.e. a 'normal' virtual function). A class provides the implementation for this function, but its derived class can override this implementation by providing its own implementation for this method.

    0 讨论(0)
提交回复
热议问题