Should the base class executable functions be 0'd?

which of the following recommendations is better?

class Foo {
virtual void unimplementedFunc() = 0;
};

or

     class Foo {
        virtual void unimplementedFunc();
        };

//in cpp
void Foo::unimplementedFunc()
{
   //not implemented in base
}

thank

+3
source share
4 answers
class Foo {
virtual void unimplementedFunc() = 0;
};

You have a pure virtual function inside Foo, so Foo is an abstract class. In other words, you cannot create an instance Foo. Most derived classes are needed to provide the implementation of a virtual function.

On the other hand, the second version declares a virtual function (not pure) and defines it outside the class. Derived classes can redefine a function to achieve the desired behavior.

. .

class Foo {
    virtual void unimplementedFunc() = 0;
    };

void Foo::unimplementedFunc() {} //is valid
+1

. ? ?

, , .

+3

, , . , Foo . .

+1

, = 0 (i.e) Base:: foo() - ( "= 0" ). *

, .

, . . , , , .

.

: http://www.glenmccl.com/tip_033.htm

+1
source

Source: https://habr.com/ru/post/1769675/


All Articles