简体   繁体   中英

Overriding C++ pure virtual functions

I have the following classes:

Mode class h-file:

#pragma once

class Mode
{

public: 
    virtual int recv()  = 0;
};

Mode class cpp-file: -> empty

LocalMode class h-file:

#pragma once
#include "Mode.h"

class LocalMode: public Mode
{
private: 

public: 
    LocalMode();
    int recv();

};

LocalMode class cpp-file:

#include "LocalMode.h"

int LocalMode::recv(){
    return 0;
}

Here are my questions:

  1. Is the keyword "override" always necessary? If not, what are the best practices?

  2. The main question: I know the code above works for me. But I have the problem, that I basically have to "copy" the function signature of the pure virtual function out of the base class into my derived class. What happens, if I don't know what pure virtual functions the base class has?

    My implementation above implies that I have to know all the pure virtual functions available in the base class. I tried accessing the pure virtual function by the Mode:: scope and LocalMode:: scope but in Visual Studio I simply got some error messages (I deem those error messages to be rather irrelevant to this question).

  3. Some plug-ins/Intellisense? I remember that in java IntelliSense in general helped me out and added the needed functions from the abtract class. Although I am aware that java is a bit different in that sense (inheriting from abtract classes) to c++, I would also like if there are any tools which help me to include those automatically?

Skiming through the internet I could not find any examples. They all assume, that all the pure virtual functions of the base class are known... I am just imaging that if I had a abstract class with a lot of pure virtual functions and I forgot to copy just one of them, when instantiating I would get an error...

Thank you in advance.

Is the keyword "override" always necessary? If not, what are the best practices?

It is never "necessary". Overriding works with or without this keyword. It is only here to help you prevent issues like typos etc.

struct A
{
    virtual int foo();
};

struct B: public A
{
    int fooo(); //whoops, not overriding, no compiler error
};

struct C: public A
{
    int fooo() override; //compiler error, compiler noticed my typo
};

So, the best practice is to always use override keyword when you want to override a virtual function.


The main question: I know the code above works for me. But I have the problem, that I basically have to "copy" the function signature of the pure virtual function out of the base class into my derived class. What happens, if I don't know what pure virtual functions the base class has?

You cannot not know that. To derive from class, compiler requires full definition of that class, which typically means you have #include d it (and you have access to that definition).


My implementation above implies that I have to know all the pure virtual functions available in the base class. I tried accessing the pure virtual function by the Mode:: scope and LocalMode:: scope but in Visual Studio I simply got some error messages (I deem those error messages to be rather irrelevant to this question).

Are you looking for reflection mechanism? It's not present in C++, so you cannot eg get a list of function for given class. If you want to call pure virtual function from another function, that cannot work because, well, they are pure virtual.


Some plug-ins/Intellisense?

That is explicitly off-topic for StackOverflow, but there are many IDEs for C++, you shouldn't have any trouble finding them.

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