C++:如何使用未定义的类型?

发布于 2024-09-03 22:17:01 字数 957 浏览 6 评论 0原文

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 Bunny has not been completely defined at the point where I use it in its own class definition. because I did something stupid (unrelated).

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ペ泪落弦音 2024-09-10 22:17:01

您的问题的答案取决于参考文献<>的内容。看起来像。如果它有一个 Bunny 类型的实例变量,那么它当然不会工作(怎么会呢,你有一个永远不会结束的递归定义)。如果它只有引用和指针,那么它应该可以正常工作。模板实例化中的 Bunny 类型不会干扰这一点。

编辑(发布参考<>代码编辑):

我似乎无法重现您的问题。我已经重新实现了像您正在做的那样的代码,但它对我来说编译得很好:

struct base {
  void fun() {}
};
template < typename T >
struct temp
{
  T * t;

  void f() { ((base*)t)->fun(); }
};

struct test
{
  temp<test> t;

};

int main()
{
  test t;
  t.t.f();
}

这显然是无效的代码,因为您将得到未定义的结果,但它确实可以编译。这里的主要问题是从类型 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:

struct base {
  void fun() {}
};
template < typename T >
struct temp
{
  T * t;

  void f() { ((base*)t)->fun(); }
};

struct test
{
  temp<test> t;

};

int main()
{
  test t;
  t.t.f();
}

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.

装纯掩盖桑 2024-09-10 22:17:01

如果 Reference 具有 T 类型的变量,则 T 不能具有 Reference 类型的变量代码>.您的替代方案是重写 Reference,或重写 Bunny 以使用指向 Reference 的指针:

template<class> class Reference;

class Bunny
{
    ...
private:
    Reference<Bunny>* parent;
}

If Reference<T> has a variable of type T, then T cannot have a variable of type Reference<T>. Your alternatives are either rewriting Reference<T>, or rewriting Bunny to use a pointer to Reference<T>:

template<class> class Reference;

class Bunny
{
    ...
private:
    Reference<Bunny>* parent;
}
八巷 2024-09-10 22:17:01

这不就是一个抽象的问题,抽象成为问题的解决方案吗?我会考虑创建一个接口 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 use Reference<IBunny> inside any definition that implements IBunny.

This is one of those use-cases that interfaces were invented for (comes in handy in typical GoF creational patterns).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文