解决模板类之间的圆相关性 [英] Resolving a Circular Dependency between Template Classes

查看:179
本文介绍了解决模板类之间的圆相关性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类 Foo Bar ,派生自 Base 。每个都覆盖一个方法 virtual Base * convert(ID)const ,其中 ID 特定的实例化 Foo Bar (假设它是一个枚举)。问题是 Foo :: convert()需要能够返回 Bar 实例,同样 Bar :: convert()需要能够实例化 Foo 。由于它们都是模板,这导致 Foo.h Bar.h 之间的循环依赖关系。

编辑:转发声明不起作用,因为每个方法的实现都需要其他类的构造函数:



Foo.h

  #include< Base.h> 

template< class T>类Bar;

template< class T>
class Foo:public Base {...};

template< class T>
Base * Foo< T> :: convert(ID id)const {

if(id == BAR_INT)
return new Bar< int>(value); //错误。

...

}

code> Bar.h :

  #include< Base.h> 

template< class T> Foo类;

template< class T>
class Bar:public Base {...};

template< class T>
Base * Bar< T> :: convert(ID id)const {

if(id == FOO_FLOAT)
return new Foo< float>(value) //错误。

...

}

错误当然是无效使用不完全类型。

解决方案

你需要做的是将类声明实施。所以像

 模板< class T> class Foo:public Base 
{
public:
Base * convert(ID)const;
}

template< class T> class Bar:public Base
{
public:
Base * convert(ID)const;
}

template< class T> Base * Foo< T> :: convert(ID)const {return new Bar< T> ;;
template< class T> Base * Bar< T> :: convert(ID)const {return new Foo T ;;

这样,当定义函数时,就有了完整的类定义。


I have two classes, Foo<T> and Bar<T>, derived from Base. Each overrides a method virtual Base* convert(ID) const, where ID is an instance of a type that uniquely identifies a particular instantiation of Foo or Bar (pretend it's an enum). The problem is that Foo::convert() needs to be able to return a Bar instance, and likewise Bar::convert() needs to be able to instantiate Foo. Since they're both templates, this results in a circular dependency between Foo.h and Bar.h. How do I resolve this?

Edit: A forward declaration does not work because the implementation of each method needs the constructor of the other class:

Foo.h:

#include <Base.h>

template<class T> class Bar;

template<class T>
class Foo : public Base { ... };

template<class T>
Base* Foo<T>::convert(ID id) const {

    if (id == BAR_INT)
        return new Bar<int>(value); // Error.

    ...

}

Bar.h:

#include <Base.h>

template<class T> class Foo;

template<class T>
class Bar : public Base { ... };

template<class T>
Base* Bar<T>::convert(ID id) const {

    if (id == FOO_FLOAT)
        return new Foo<float>(value); // Error.

    ...

}

The error is, naturally, "invalid use of incomplete type".

解决方案

What you need to do is seperate the class declarations from the implementation. So something like

template <class T> class Foo : public Base
{
    public:
    Base* convert(ID) const;
}

template <class T> class Bar : public Base
{
    public:
    Base* convert(ID) const;
}

template <class T> Base* Foo<T>::convert(ID) const {return new Bar<T>;}
template <class T> Base* Bar<T>::convert(ID) const {return new Foo<T>;}

This way, you have complete class definitions when the functions are defined.

这篇关于解决模板类之间的圆相关性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆