两个模板类由彼此的成员组成 [英] Two template classes being composed of a member of each other

查看:137
本文介绍了两个模板类由彼此的成员组成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要在我的代码中两个模板类由彼此的成员字段组成。例如,我有两个文件,



templates.h

  template< class T> B类; 

template< class T>
class A
{
B< A>一个;

//依赖于T
的字段和方法};

template< class T>
class B
{
A< B> b;

//依赖于T
的字段和方法};

main.cpp

  #includetemplates.h

int main()
{
A< int>一个;
}

编译时接收到此链接中显示的输出



http://pastebin.com/taBWZjar



我使用的是g ++编译器。当我输入g ++ --version,我得到


g ++(Gentoo 4.7.2 p1.3,pie-0.5.5) 2


如果这在c ++中不可能做,那么什么是替代方法或解决方案?

解决方案

您当前的设计包含一个严重的缺陷,称为 circular依赖。它永远不会编译,解决它的最好的方法是重新设计你的类层次结构。



为了给你一个线索为什么它永远不能编译,让简化的情况删除模板:

 类B; 

A类{
B b;
};

class B {
A a;
};

现在试着把它想象成一个编译器。要知道在编译时分配 A (和编译器必须知道)需要多少空间, ),它需要知道需要多少空间来分配类 B ,反之亦然。显然,这是循环依赖



一个可能的解决方法是切换到指针(或引用):

  B类; 

A类{
B * b; // B& b;可能太
};

class B {
A a;
};

这应该编译就好了。原因是现在编译器知道 b 是一个指针,并且容纳该指针所需的空间是固定的(对于32位目标为4字节,对于32位目标为8字节)

注意:指向 a 的指针/引用 B 是不必要的,因为 A 已在此时定义,因此我们可以将其用作 B



在你的情况下,你进一步复杂的模板,而你可以利用建议的修复指针/引用)并将其与模板组合,我不推荐它。您的代码必须重新设计。


I have a need in my code for two template classes to be composed of a member field of each other. For example, I have two files,

templates.h

template <class T> class B;

template <class T>
class A
{
    B<A> a;

    // fields and methods dependent on T
};

template <class T>
class B
{
    A<B> b;

    // fields and methods dependent on T
};

main.cpp

#include "templates.h"

int main()
{
    A<int> a;
}

When I compile I receive the output shown in this link

http://pastebin.com/taBWZjar

I am using the g++ compiler. When I type g++ --version, I get

g++ (Gentoo 4.7.2 p1.3, pie-0.5.5) 4.7.2

If this is not possible to do in c++, what is an alternative or work around? Or perhaps is this a bug with my compiler?

解决方案

Your current design contains a serious flaw which is known as circular dependency. It will never compile, and the best way to fix it is to redesign your class hierarchy.

To give you the clue why it can never compile, lets simplify the situation by removing templates:

class B;

class A {
  B b;
};

class B {
  A a;
};

Now try to think as a compiler. To know how much space is needed for allocation of class A (and compiler has to know that at compile-time), it needs to know how much space is needed for allocation of class B, and vice versa. Clearly, this is a circular dependency. You can try to compile it yourself and see that compiler complains about it.

One possible way to fix that would be switching to pointers (or references):

class B;

class A {
  B* b; // B& b; is possible too
};

class B {
  A a;
};

This should compile just fine. The reason is that now the compiler knows that b is a pointer, and the space needed to accommodate that pointer is fixed (4 bytes for 32-bit targets, and 8 bytes for 64-bit targets accordingly).

NOTE: Pointer/reference to a in B is unnecessary because A is already defined at that point, and therefore we can use it as a direct member in B.

In your situation you complicated things even further with templates, and while you could utilize the proposed fix (using pointers/references) and combine it with templates, I don't recommend it. Your code has to be totally redesigned.

这篇关于两个模板类由彼此的成员组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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