c++ - 模板继承层次结构中的循环依赖

标签 c++ templates c++11 c++14 circular-dependency

我有一个模板化的基类,它有多个子类并提供一个抽象的执行方法。子级以不同的方式实现此方法,并且可以将执行调用委托(delegate)给 Base 类的尾部 vector 中的对象。 为了将对象链接到尾部,Base 类提供了一些方法(createChild1、createChild2)。这是代码:

基础.h

#pragma once

#include <memory>
#include <vector>

template<typename T>
class Child1;

template<typename T>
class Child2;

template<typename T>
class Base {
public:
    std::vector<std::unique_ptr<Base<T>>> tail;

    virtual void execute(const T &t) = 0;

    Base<T> &createChild1() {
        auto child = std::make_unique<Child1<T>>();
        tail.push_back(std::move(child));
        return *tail.back().get();
    }

    Base<T> &createChild2() {
        auto child = std::make_unique<Child2<T>>();
        tail.push_back(std::move(child));
        return *tail.back().get();
    }
};

child1.h

#pragma once

#include "base.h"

template<typename T>
class Child1 : public Base<T> {
public:
    virtual void execute(const T &t) {
        // do something with t
    }
};

child2.h

#pragma once

#include "base.h"

template<typename T>
class Child2 : public Base<T> {
public:
    virtual void execute(const T &t) {
        // do something with t
        // then delegate to t
        for (auto &next : tail) {
            next->execute(t);
        }
    }
};

主要.cpp

#include <iostream>

#include "child1.h"

using namespace std;

int main(int argc, char **argv) {
    Child1<int> c;
    c.createChild2().createChild1();
    c.execute(10);
    return 0;
}

如果我尝试编译,我会得到一个“未定义模板‘Child2’的隐式实例化”,因为在 Base 中,模板类 Child2 只是前向声明的,此时它的主体是未知的。在 Child1 和 Child2 前面声明 Base 并将 Child1 和 Child2 的定义包含在 Base 中并不能解决问题,因为这样 Child2 就无法访问 tail。 我该如何解决这种循环依赖? Child1、Child2 和 Base 中的代码可以更改,但我想保留在 main 中链接创建调用的可能性(因此创建方法必须在 Base 类中声明)。

最佳答案

解决方案是在访问 tail 之前加上 this-> 前缀。因此只需更改 child2.h:

#pragma once

#include "base.h"

template<typename T>
class Child2 : public Base<T> {
public:
    virtual void execute(const T &t) {
        // do something with t
        // then delegate to t
        for (auto &next : this->tail) {
            next->execute(t);
        }
    }
};

关于c++ - 模板继承层次结构中的循环依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36292687/

相关文章:

templates - 如何在 go 模板中将 "or"与管道一起使用?

c++ - 在命名空间中声明变量,在 main 中定义它,使其对所有其他文件可见

c++ - 模式匹配 - 在第二张图像中查找引用对象 [OpenCV?]

c++ - 复制控制函数中如何处理 C++ 数组成员?

c++ - 为什么双重优先于 float ?

c++ - 专用于 "direct"函数类型(与函数指针类型相反)

c++ - 捕获 EXCEPTION_GUARD_PAGE 是否安全

c++ - 我不明白为什么这个模板特化在 VS 2010 中失败

c++ - 项目的 Makefile

c++ - 合理支持的最低 GCC 版本是多少?