[英]Cyclic Template
Suppose template class A is defined as follows: 假设模板类A定义如下:
template <typename T>
class A
{
B<T> b;
};
Suppose template class B is defined as follows: 假设模板类B定义如下:
template <typename T>
class B
{
A<T> a;
};
Ideally, these classes would be defined in separate headers with inlined implementations. 理想情况下,这些类将在具有内联实现的单独标头中定义。 However, this would cause a cyclic inclusion problem.
但是,这会导致循环包含问题。 One solution is to put the implementation of the templated classes into a cpp file.
一种解决方案是将模板化类的实现放入cpp文件中。 However, this would require specialized template instantiations.
但是,这需要专门的模板实例化。
Is there a way to keep the implementation of the classes inlined and avoid a cyclic inclusion dependency? 有没有办法保持内联类的实现并避免循环包含依赖? I would like to avoid using pointers if it is possible.
如果可能的话,我想避免使用指针。
Thanks, 谢谢,
Sam 山姆
Forward declare your classes and use header guards (or non-standard #pragma once). 转发声明您的类并使用标题保护(或非标准#pragma一次)。 http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.11
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.11
Note that two classes cannot fully contains an object of each other. 请注意,两个类不能完全包含彼此的对象。 This would describe an object of infinite size...
这将描述一个无限大小的物体......
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.12 http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.12
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.