c++ - 一个类 C++ (Arduino) 中的多个头文件

标签 c++

我有一个类包含来自不同头文件的方法,如下所示:

#include "ICash.h"
#include "ILock.h"

class control: public ICash, public ILock
{
  public:
      control();

  private:
     void doSomething(int value);

};

所以现在当我通过控制类创建 ICash 的实例时。 在 control.cpp 中声明方法的位置。

是否可以通过 ICash 接口(interface)使用控件中的 doSomething 方法?

谢谢。

最佳答案

例如在control.cpp中,你可以有这样的实现代码:

control::control() { /* ctor body */ }

void control::doSomething(int value)
{
    // your code here
}

然后,在某个地方,您可以通过指向 ICash 的指针使用 control 实例

ICash *cash = new control;

如果 doSomething 不是 private,你可以使用 void doSomething(int) 从这个指针 cashcash->doSomething(5) 仅当 ICash 也有一个声明(虚拟或非虚拟)用于 void doSomething(int) 时。

所以简而言之,要能够在 ICash 中使用 doSomething,它也必须在 ICash 中声明。

请注意,当 doSomethingICash 中声明并且您在 control 中重新定义方法(覆盖)时,您需要在 中声明>ICash 成为虚拟

关于c++ - 一个类 C++ (Arduino) 中的多个头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19517638/

相关文章:

c++ - 错误 : redefinition of class [class name]

c++ - 删除 C++ 中的尾随逗号

c++ - 奇怪的 std::wregex 行为

c++ - 在 Qt 中使用 SQLite 自定义函数

c++ - 可以在 Debian Bullseye 上使用 g++ (gcc) 11 吗?

C++:修复类模板特化/继承问题

c++ - 如何将 float 转换为字符串

c++ - 为什么 VisualStudio 要查找这个 lib 文件? LNK1104错误

c++ - Qt 是否有自己的 boost::optional 替代方案?

c++ - 将 std::string 的一部分复制到另一个未初始化的字符串