C++:指向包含子级的类的父级指针

发布于 2024-12-17 00:00:02 字数 583 浏览 3 评论 0原文

我有一个可以用这个例子简化的类设计问题:

// foo.h
#include "foo2.h" 
class foo
{
public:
    foo2 *child;
// foo2 needs to be able to access the instance
// of foo it belongs to from anywhere inside the class
// possibly through a pointer
};

// foo2.h
// cannot include foo.h, that would cause an include loop

class foo2
{
public:
    foo *parent;
// How can I have a foo pointer if foo hasn't been pre-processed yet?
// I know I could use a generic LPVOID pointer and typecast later
// but isn't there a better way?
};

除了使用通用指针或将父指针传递给 foo2 成员的每次调用之外,还有其他方法吗?

I have a class design problem that could simplified with this example:

// foo.h
#include "foo2.h" 
class foo
{
public:
    foo2 *child;
// foo2 needs to be able to access the instance
// of foo it belongs to from anywhere inside the class
// possibly through a pointer
};

// foo2.h
// cannot include foo.h, that would cause an include loop

class foo2
{
public:
    foo *parent;
// How can I have a foo pointer if foo hasn't been pre-processed yet?
// I know I could use a generic LPVOID pointer and typecast later
// but isn't there a better way?
};

Is there any other way other than using a generic pointer or passing the parent pointer to every call of foo2 members?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

血之狂魔 2024-12-24 00:00:02

如果您只使用指针,则不需要包含该文件,并且如果将它们包含在 .cpp 文件中,则不会遇到循环问题:

// foo.h
class foo2; // forward declaration
class foo
{
public:
    foo2 *child;
};

// foo2.h
class foo;
class foo2
{
public:
    foo *parent;
};

//foo.cpp
#include "foo.h"
#include "foo2.h"

//foo2.cpp
#include "foo2.h"
#include "foo.h"

尽管重新考虑设计可能会更好。

You don't need to include the file if you're only using a pointer, and you won't have looping trouble if you include them in .cpp files:

// foo.h
class foo2; // forward declaration
class foo
{
public:
    foo2 *child;
};

// foo2.h
class foo;
class foo2
{
public:
    foo *parent;
};

//foo.cpp
#include "foo.h"
#include "foo2.h"

//foo2.cpp
#include "foo2.h"
#include "foo.h"

Although you may be better off by rethinking your design.

浅语花开 2024-12-24 00:00:02

前向声明是一个朋友:

// foo.h
class foo2;

class foo
{
  foo2 *pFoo2;
};

// foo2.h
#include "foo.h"
class foo2
{
  foo *pFoo;
};

不过,正如 Pubby 所说,需要相互了解的类可能应该只是一个类,或者可能是一个具有两个成员的类,这两个成员都知道父类,但不能作为两个成员方式关系。

就为人父母和通用而言:

template  <class Parent>
class  ChildOf
{
public:
  // types
  typedef Parent  ParentType;

  // structors
  explicit ChildOf(Parent& p);
  ~ChildOf();

  // general use
  Parent&  GetParent();
  const Parent&  GetParent() const;

  void  SetParent(Parent& p);

private:
  // data
  Parent  *m_pParent;
};

/*
  implementation
*/
template  <class ParentType>
ChildOf<ParentType>::ChildOf(ParentType& p)
: m_pParent(&p)
{}

template  <class Parent>
ChildOf<Parent>::~ChildOf()
{}

template  <class ParentType>
inline
ParentType&  ChildOf<ParentType>::GetParent()
{
  return *m_pParent;
}

template  <class ParentType>
inline
const ParentType&  ChildOf<ParentType>::GetParent() const
{
  return *m_pParent;
}

template  <class ParentType>
void  ChildOf<ParentType>::SetParent(ParentType& p)
{
  m_pParent = &p;
}

Forward declaration is a friend:

// foo.h
class foo2;

class foo
{
  foo2 *pFoo2;
};

// foo2.h
#include "foo.h"
class foo2
{
  foo *pFoo;
};

As Pubby says, though, classes that need to know about each other should probably just be one class, or maybe a class, with two members, both of which know about the parent class, but not as a two-way relationship.

As far as parenthood and being generic goes:

template  <class Parent>
class  ChildOf
{
public:
  // types
  typedef Parent  ParentType;

  // structors
  explicit ChildOf(Parent& p);
  ~ChildOf();

  // general use
  Parent&  GetParent();
  const Parent&  GetParent() const;

  void  SetParent(Parent& p);

private:
  // data
  Parent  *m_pParent;
};

/*
  implementation
*/
template  <class ParentType>
ChildOf<ParentType>::ChildOf(ParentType& p)
: m_pParent(&p)
{}

template  <class Parent>
ChildOf<Parent>::~ChildOf()
{}

template  <class ParentType>
inline
ParentType&  ChildOf<ParentType>::GetParent()
{
  return *m_pParent;
}

template  <class ParentType>
inline
const ParentType&  ChildOf<ParentType>::GetParent() const
{
  return *m_pParent;
}

template  <class ParentType>
void  ChildOf<ParentType>::SetParent(ParentType& p)
{
  m_pParent = &p;
}
撩发小公举 2024-12-24 00:00:02

您应该使用前向声明并将头文件包含在您的 cpp 中

// foo.h
#ifndef FOO_H_
#define FOO_H_
class foo2;

class foo
{
public:
    foo2 *child;
};
#endif

// foo2.h
#ifndef FOO_2_H_
#define FOO_2_H_
class foo;

class foo2
{
public:
    foo *parent;
};
#endif

You should use forward declarations and include the header files in your cpp

// foo.h
#ifndef FOO_H_
#define FOO_H_
class foo2;

class foo
{
public:
    foo2 *child;
};
#endif

// foo2.h
#ifndef FOO_2_H_
#define FOO_2_H_
class foo;

class foo2
{
public:
    foo *parent;
};
#endif
心凉怎暖 2024-12-24 00:00:02

使用 前向声明 告诉编译器 foo2是一个随后定义的类。

class foo2;

class foo {
    foo2 *child;
};

class foo2 {
    foo *parent;
};

Use a forward declaration to tell the compiler that foo2 is a class that will be defined subsequently.

class foo2;

class foo {
    foo2 *child;
};

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