覆盖 C++ 纯虚函数
Overriding C++ pure virtual functions
我有以下 classes:
模式class
h-文件:
#pragma once
class Mode
{
public:
virtual int recv() = 0;
};
模式class
cpp 文件:
-> 空
本地模式class
h-文件:
#pragma once
#include "Mode.h"
class LocalMode: public Mode
{
private:
public:
LocalMode();
int recv();
};
本地模式class
cpp 文件:
#include "LocalMode.h"
int LocalMode::recv(){
return 0;
}
这是我的问题:
关键字 "override" 总是必需的吗?如果不是,最佳做法是什么?
主要问题:
我知道上面的代码对我有用。但是我有一个问题,我基本上必须 "copy" 将基 class 中的纯虚函数的函数签名 class 放入我的派生 class 中。如果我不知道基 class 有哪些纯虚函数会怎样?
我上面的实现意味着我必须知道基础 class 中可用的所有纯虚函数。
我尝试通过 Mode:: scope 和 LocalMode:: scope 访问纯虚函数,但在 Visual Studio 中我只是收到了一些错误消息(我认为这些错误消息与这个问题无关)。
有些plug-ins/Intellisense?
我记得在 java 中,IntelliSense 通常帮助我解决问题并从抽象 class 中添加了所需的功能。虽然我知道 java 在这个意义上(继承自抽象 classes)与 c++ 有点不同,但我也想知道是否有任何工具可以帮助我自动包含这些工具?
在互联网上浏览我找不到任何例子。他们都假设,基础 class 的所有纯虚函数都是已知的......
我只是在想象,如果我有一个带有很多纯虚函数的抽象 class 并且我忘记只复制其中一个,那么在实例化时我会得到一个错误......
提前谢谢你。
Is the keyword "override" always necessary? If not, what are the best practices?
永远不会"necessary"。使用或不使用此关键字都可以覆盖。它只是为了帮助您防止拼写错误等问题
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
};
因此,最佳做法是在要覆盖虚函数时始终使用 override
关键字。
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?
你不可能不知道。要从 class 派生,编译器需要 class 的完整定义,这通常意味着您拥有 #include
d 它(并且您可以访问该定义)。
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).
您在寻找反射机制吗?它不存在于 C++ 中,所以你不能,例如获取给定 class 的函数列表。如果你想从另一个函数调用纯虚函数,那是行不通的,因为它们是纯虚函数。
Some plug-ins/Intellisense?
那是 Whosebug 的 explicitly off-topic,但是 C++ 的 IDE 有很多,您应该可以轻松找到它们。
我有以下 classes:
模式class h-文件:
#pragma once
class Mode
{
public:
virtual int recv() = 0;
};
模式class cpp 文件: -> 空
本地模式class h-文件:
#pragma once
#include "Mode.h"
class LocalMode: public Mode
{
private:
public:
LocalMode();
int recv();
};
本地模式class cpp 文件:
#include "LocalMode.h"
int LocalMode::recv(){
return 0;
}
这是我的问题:
关键字 "override" 总是必需的吗?如果不是,最佳做法是什么?
主要问题: 我知道上面的代码对我有用。但是我有一个问题,我基本上必须 "copy" 将基 class 中的纯虚函数的函数签名 class 放入我的派生 class 中。如果我不知道基 class 有哪些纯虚函数会怎样?
我上面的实现意味着我必须知道基础 class 中可用的所有纯虚函数。 我尝试通过 Mode:: scope 和 LocalMode:: scope 访问纯虚函数,但在 Visual Studio 中我只是收到了一些错误消息(我认为这些错误消息与这个问题无关)。
有些plug-ins/Intellisense? 我记得在 java 中,IntelliSense 通常帮助我解决问题并从抽象 class 中添加了所需的功能。虽然我知道 java 在这个意义上(继承自抽象 classes)与 c++ 有点不同,但我也想知道是否有任何工具可以帮助我自动包含这些工具?
在互联网上浏览我找不到任何例子。他们都假设,基础 class 的所有纯虚函数都是已知的...... 我只是在想象,如果我有一个带有很多纯虚函数的抽象 class 并且我忘记只复制其中一个,那么在实例化时我会得到一个错误......
提前谢谢你。
Is the keyword "override" always necessary? If not, what are the best practices?
永远不会"necessary"。使用或不使用此关键字都可以覆盖。它只是为了帮助您防止拼写错误等问题
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
};
因此,最佳做法是在要覆盖虚函数时始终使用 override
关键字。
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?
你不可能不知道。要从 class 派生,编译器需要 class 的完整定义,这通常意味着您拥有 #include
d 它(并且您可以访问该定义)。
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).
您在寻找反射机制吗?它不存在于 C++ 中,所以你不能,例如获取给定 class 的函数列表。如果你想从另一个函数调用纯虚函数,那是行不通的,因为它们是纯虚函数。
Some plug-ins/Intellisense?
那是 Whosebug 的 explicitly off-topic,但是 C++ 的 IDE 有很多,您应该可以轻松找到它们。