Why should I specify pure virtual functions in a derived class declaration in Visual C ++?

Given the base class A and derived class B:

class A { public: virtual void f() = 0; }; class B : public A { public: void g(); }; void B::g() { cout << "Yay!"; } void B::f() { cout << "Argh!"; } 

I get errors saying that f () is not declared in B while trying to define void B :: f (). Should I declare f () explicitly in B? I think that if the interface changes, I don’t have to fix the declarations in each individual class that derives from it. Is there no way for B to automatically get all virtual function declarations from A?

EDIT: I found an article that says that inheriting pure virtual functions depends on the compiler: http://www.objectmentor.com/resources/articles/abcpvf.pdf

I am using VC ++ 2008, I wonder if there is an option for this.

+4
source share
6 answers

Yes, in C ++ you should explicitly clarify your intention to override the behavior of a base class method by declaring (and defining) it in a derived class. If you try to provide a new implementation in a derived class without declaring it in the class definition, this will be a compiler error.

+5
source

Should I declare f () explicitly in B?

Yes , you must declare in the class definition the entire virtual function of any base classes that you want to override in the class. As for why: This is just a C ++ syntax way.
Note that the virtual may be omitted to declare overridden virtual functions:

 class base { virtual void f(); virtual void g(); }; class derived : public base { virtual void f(); // overrides base::f() void g(); // overrides base::g() }; 

Note. Class declaration: class my_class; , and this is class my_class { /* ... */ }; - class definition. There is a limited number of things you can do with a class that has been declared but not defined. In particular, you cannot instantiate or call member functions.
See here for more details on the differences between declarations and definitions.

Well, in the interest of discussing the “definition versus definition” occurring in the comments, here is a quote from the C ++ 03 standard, 3.1 / 2:

A declaration is a definition if it is [...] a declaration of the class name [...].

3.1 / 3 gives some examples. Among them:

[Example: [...]
struct S { int a; int b; }; // defines S, S::a, and S::b
[...]
struct S; // declares S
-end example]

To summarize: the C ++ standard considers struct S; like declaration and struct S { /*...*/ }; definition. I find this a strong backup of my interpretation of the declaration versus definition for C ++ classes.

+7
source

A C ++ class declaration defines the contents of a class. If you do not declare f () in B, it looks like you are not overriding it. B::f() can only be implemented if you declare it.

+2
source

In your current code, you simply inherit the f () function in class B and do not override the base class member in the derived class.

0
source

Is there no way for B to get all virtual function declarations from A automatically?

If you do not mark function f as pure virtual c = 0, the function is also automatically present in any subclass.

 class A { public: virtual void f(); // not =0! }; class B : public A { public: void g(); }; void A::f() { cout << "I am A::f!"; } void B::g() { cout << "Yay!"; } 

Now:

 B* b = new B(); b->f(); // calls A::f 
0
source

By declaring a pure virtual function, you declare that your class is abstract and that you want all specific derived classes to have an implementation of this function. A derived class that does not provide an implementation for a pure virtual function is an extension of an abstract base class and is itself an abstract class. Of course, trying to create an abstract class is a mistake.

Pure virtual functions let you define the “contract” interface that you expect from all derived classes. A client of this class can expect any instance of a class with this interface to implement functions in the contract.

Another interesting tidbit ... you can provide the body with a pure virtual function, but it is still clean and needs to be redefined in a specific derived class. The advantage of supplying the body is to provide basic behavior while at the same time forcing derived classes to perform this function. Overridden functions can then call the base function Base :: F (), like other virtual functions. When the body is not defined, calling Base :: F () in pure virtual functions is an error.

0
source

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


All Articles