C++:如何使用未定义的类型?
C++ 要求在使用所有类型之前先定义它们,这使得以正确的顺序包含头文件变得很重要。美好的。但是我的情况呢:
Bunny.h
:
class Bunny { ... private: Reference<Bunny> parent; }
编译器抱怨,因为技术上因为我做了一些愚蠢的事情(不相关)。Bunny
在我在自己的类中使用它时尚未完全定义定义。
除了重写我的模板类 Reference
使其采用指针类型(在这种情况下我可以使用 Bunny
的前向声明),我不知道如何解决这个问题。
有什么建议吗?
编辑:我的Reference
类(XObject
是数据模式对象的基类):
template <class T = XObject> class Reference { public: Reference() : m_ptr (NULL) {} Reference(T* p) { m_ptr = p; if (p != NULL) ((XObject*)p)->ref(); } ~Reference() { if (m_ptr) { ((XObject*)m_ptr)->deref(); } } // ... assignment, comparison, etc. private: T* m_ptr; };
编辑:这工作正常,问题是别的东西。非常感谢您的帮助!
C++ requires all types to be defined before they can be used, which makes it important to include header files in the right order. Fine. But what about my situation:
Bunny.h
:
class Bunny { ... private: Reference<Bunny> parent; }
The compiler complains, because technically because I did something stupid (unrelated).Bunny
has not been completely defined at the point where I use it in its own class definition.
Apart from re-writing my template class Reference
so it takes a pointer type (in which case I can use the forward declaration of Bunny
), I don't know how to solve this.
Any suggestions?
EDIT: My Reference
class (XObject
is a base class for data mode objects):
template <class T = XObject> class Reference { public: Reference() : m_ptr (NULL) {} Reference(T* p) { m_ptr = p; if (p != NULL) ((XObject*)p)->ref(); } ~Reference() { if (m_ptr) { ((XObject*)m_ptr)->deref(); } } // ... assignment, comparison, etc. private: T* m_ptr; };
EDIT: This works fine, the problem was something else. Thanks so much for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题的答案取决于参考文献<>的内容。看起来像。如果它有一个 Bunny 类型的实例变量,那么它当然不会工作(怎么会呢,你有一个永远不会结束的递归定义)。如果它只有引用和指针,那么它应该可以正常工作。模板实例化中的 Bunny 类型不会干扰这一点。
编辑(发布参考<>代码编辑):
我似乎无法重现您的问题。我已经重新实现了像您正在做的那样的代码,但它对我来说编译得很好:
这显然是无效的代码,因为您将得到未定义的结果,但它确实可以编译。这里的主要问题是从类型 test* 到类型 base* 的重新解释。如果测试确实从基类继承,那么甚至不需要强制转换。这样的代码不会按预期运行,但应该可以正常编译。我的一个建议是放弃所有 c 风格的强制转换。但这并不能解决您遇到的问题,无论它是什么......它必须位于您未粘贴的代码中的某个位置。
The answer to your question depends on what Reference<> looks like. If it has an instance variable of type Bunny in it then of course it won't work (how would it, you have a recursive definition that never ends). If it has only references and pointers in it then it should work fine. The Bunny type in the template instantiation would not interfere with this.
Edit (post reference<> code edit):
I can't seem to recreate your problem. I've reimplemented code like what you're doing but it compiles fine for me:
It's obviously invalid code in that you're going to get undefined results but it does compile. Mainly the issue here is the reinterpret cast from type test* to type base*. If test really did inherit from base then the cast wouldn't even be necessary. Code like this will not function as expected but it should compile just fine. One recommendation I'd have is to lose all the c-style casts. That won't solve the problem you're having though, whatever it is...it's got to be somewhere in the code you're not pasting.
如果
Reference
具有T
类型的变量,则T
不能具有Reference
类型的变量代码>.您的替代方案是重写Reference
,或重写Bunny
以使用指向Reference
的指针:If
Reference<T>
has a variable of typeT
, thenT
cannot have a variable of typeReference<T>
. Your alternatives are either rewritingReference<T>
, or rewritingBunny
to use a pointer toReference<T>
:这不就是一个抽象的问题,抽象成为问题的解决方案吗?我会考虑创建一个接口
IBunny
,然后在实现IBunny
的任何定义内使用Reference
。这是接口被发明的用例之一(在典型的 GoF 创建模式中派上用场)。
Isn't this merely a matter of abstraction, where abstraction becomes the solution to the problem? I'd consider creating an interface,
IBunny
, and then useReference<IBunny>
inside any definition that implementsIBunny
.This is one of those use-cases that interfaces were invented for (comes in handy in typical GoF creational patterns).