解决循环依赖关系 c++ 的想法
Ideas in solving circular dependency c++
我在编写代码时遇到了这种问题,我该如何解决这个问题?
秒是 :
#pragma once
#include "first.h"
class second : public first
{
private:
int step;
public:
second(int value, int s) :first(value), step(s) {}
void increase() {
counter += step;
}
};
第一个h是:
#pragma once
class first
{
protected:
int counter;
public:
first():counter(0){}
first(int a) :counter(a) {}
virtual void increase() {
counter++;
}
second getequivalent(int step) {
return second(counter, step);
}
};
我的问题是,我如何获得方法
second getequivalent(int step) {
return second(counter, step);
}
在"第一"班工作?
您必须在second
的完整定义可用后实现该方法。
当您编写非模板类时,我建议将所有实现移动到单独的first.cpp
/second.cpp
文件并将它们链接在一起。
所以你的first.h
看起来像
#pragma once
class second; // Forward declaration to make compiler know that `second` is a class.
// Better option would be to #include "second.h", but you cannot
// do that because `class second` needs _full_ definition of `class first`
// because of inheritance, hence it _has_ to #include "first.h"
// without any troubles.
class first
{
protected:
int counter;
public:
first();
first(int a);
virtual void increase();
second getequivalent(int step); // Compiler don't need to know anything
// about second at this point.
};
first.cpp
看起来像
#include "first.h" // Make full definition of class first available.
#include "second.h" // Make full definition of class second available.
first::first():counter(0){}
first::first(int a) :counter(a) {}
/*virtual */ void first::increase() { // Should not specify 'virtual' in definition outside the class.
counter++;
}
second first::getequivalent(int step) {
return second(counter, step);
}
相关文章:
- C++GTKMM gui循环依赖关系
- this_thread::sleep_for和计时时钟之间的关系是否由C++11标准指定
- Doxygen - 如何在不生成图形的情况下生成文本调用关系结果
- 我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符
- 在 c++ 中对字符串进行操作的关系运算符无法按预期工作
- 如何记住这种递归关系?
- 如何在头文件中声明类模板(由于循环依赖关系)
- 移动语义和深层/浅层复制之间有什么关系?
- 如何在不知道对应关系的情况下在字符串中搜索字符并将其分配给另一个字符?
- 对在不同二进制文件中创建的对象文件的依赖关系
- #include < conio.h> 和 getch() 方法之间的关系是什么?
- 使用Bazel构建具有不同编译器/链接器选项的C/C++依赖关系
- OpenVINO - 推理库插件 libMKLDNNPlugin.so 无法解析依赖关系
- 模拟测试中类的依赖关系
- 关于记忆后这种递归关系的时间复杂度
- C++模板方法中的循环依赖关系
- 解析正交模块的依赖关系
- 超市管理系统的类别之间应该是什么关系?
- glMapBufferRange(..) 中的偏移量关系和 glDrawArraysInstanced(..) 中的第一
- 如何在 Mac OS 上安装 boost-mpi 及其对 clang 的依赖关系?