如何在c++中创建两个相互用作数据的类?

时间:2021-01-02 19:13:55

I'm looking to create two classes, each of which contains an object of the other class type. How can I do this? If I can't do this, is there a work-around, like having each class contain a pointer to the other class type? Thanks!

我希望创建两个类,每个类都包含另一个类类型的对象。我该怎么做呢?如果我做不到这一点,是否存在一种变通方法,比如让每个类包含指向另一个类类型的指针?谢谢!

Here's what I have:

这就是我有:

File: bar.h

文件:bar.h

#ifndef BAR_H
#define BAR_H
#include "foo.h"
class bar {
public:
  foo getFoo();
protected:
  foo f;
};
#endif

File: foo.h

文件:foo。

#ifndef FOO_H
#define FOO_H
#include "bar.h"
class foo {
public:
  bar getBar();
protected:
  bar b;
};
#endif

File: main.cpp

文件:main.cpp

#include "foo.h"
#include "bar.h"

int
main (int argc, char **argv)
{
  foo myFoo;
  bar myBar;
}

$ g++ main.cpp

g++ main.cpp美元

In file included from foo.h:3,
                 from main.cpp:1:
bar.h:6: error: ‘foo’ does not name a type
bar.h:8: error: ‘foo’ does not name a type

2 个解决方案

#1


76  

You cannot have two classes directly contain objects of the other type, since otherwise you'd need infinite space for the object (since foo has a bar that has a foo that has a bar that etc.)

不能有两个类直接包含另一种类型的对象,否则就需要对象的无限空间(因为foo有一个foo有一个foo有一个bar等等)。

You can indeed do this by having the two classes store pointers to one another, though. To do this, you'll need to use forward declarations so that the two classes know of each other's existence:

不过,您确实可以通过让两个类存储彼此的指针来实现这一点。为此,需要使用forward声明,以便两个类知道彼此的存在:

#ifndef BAR_H
#define BAR_H

class foo; // Say foo exists without defining it.

class bar {
public:
  foo* getFoo();
protected:
  foo* f;
};
#endif 

and

#ifndef FOO_H
#define FOO_H

class bar; // Say bar exists without defining it.

class foo {
public:
  bar* getBar();
protected:
  bar* f;
};
#endif 

Notice that the two headers don't include each other. Instead, they just know of the existence of the other class via the forward declarations. Then, in the .cpp files for these two classes, you can #include the other header to get the full information about the class. These forward declarations allow you to break the reference cycle of "foo needs bar needs foo needs bar."

注意,这两个头不包含彼此。相反,他们只是通过前面的声明知道其他类的存在。然后,在这两个类的.cpp文件中,您可以#包含其他头,以获得关于类的完整信息。这些前向声明允许您打破“foo需要bar需要foo需要foo bar”的引用循环。

#2


3  

That doesn't make sense. If A contains B, and B contains A, it would be infinite size. Imagine putting having two boxes and trying to put both into each other. Doesn't work, right?

这没有意义。如果A包含B,而B包含A,它将是无穷大。想象一下,把两个盒子放在一起,然后把两个盒子放在一起。不工作,对吧?

Pointers work though:

指针工作:

#ifndef FOO_H
#define FOO_H

// Forward declaration so the compiler knows what bar is
class bar;

class foo {
public:
  bar *getBar();
protected:
  bar *b;
};
#endif

#1


76  

You cannot have two classes directly contain objects of the other type, since otherwise you'd need infinite space for the object (since foo has a bar that has a foo that has a bar that etc.)

不能有两个类直接包含另一种类型的对象,否则就需要对象的无限空间(因为foo有一个foo有一个foo有一个bar等等)。

You can indeed do this by having the two classes store pointers to one another, though. To do this, you'll need to use forward declarations so that the two classes know of each other's existence:

不过,您确实可以通过让两个类存储彼此的指针来实现这一点。为此,需要使用forward声明,以便两个类知道彼此的存在:

#ifndef BAR_H
#define BAR_H

class foo; // Say foo exists without defining it.

class bar {
public:
  foo* getFoo();
protected:
  foo* f;
};
#endif 

and

#ifndef FOO_H
#define FOO_H

class bar; // Say bar exists without defining it.

class foo {
public:
  bar* getBar();
protected:
  bar* f;
};
#endif 

Notice that the two headers don't include each other. Instead, they just know of the existence of the other class via the forward declarations. Then, in the .cpp files for these two classes, you can #include the other header to get the full information about the class. These forward declarations allow you to break the reference cycle of "foo needs bar needs foo needs bar."

注意,这两个头不包含彼此。相反,他们只是通过前面的声明知道其他类的存在。然后,在这两个类的.cpp文件中,您可以#包含其他头,以获得关于类的完整信息。这些前向声明允许您打破“foo需要bar需要foo需要foo bar”的引用循环。

#2


3  

That doesn't make sense. If A contains B, and B contains A, it would be infinite size. Imagine putting having two boxes and trying to put both into each other. Doesn't work, right?

这没有意义。如果A包含B,而B包含A,它将是无穷大。想象一下,把两个盒子放在一起,然后把两个盒子放在一起。不工作,对吧?

Pointers work though:

指针工作:

#ifndef FOO_H
#define FOO_H

// Forward declaration so the compiler knows what bar is
class bar;

class foo {
public:
  bar *getBar();
protected:
  bar *b;
};
#endif