久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 C++ 中創建兩個相互用作數據的類?

How to create two classes in C++ which use each other as data?(如何在 C++ 中創建兩個相互用作數據的類?)
本文介紹了如何在 C++ 中創建兩個相互用作數據的類?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我希望創建兩個類,每個類都包含另一個類類型的對象.我怎樣才能做到這一點?如果我不能這樣做,是否有解決方法,例如讓每個類都包含一個指向其他類類型的指針?謝謝!

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!

這是我所擁有的:

文件:bar.h

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

文件:foo.h

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

文件:ma??in.cpp

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

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

$ 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

推薦答案

你不能讓兩個類直接包含另一種類型的對象,否則你需要為對象提供無限空間(因為 foo 有一個 barfoo 有一個酒吧等)

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.)

不過,您確實可以通過讓兩個類存儲彼此的指針來做到這一點.為此,您需要使用前向聲明,以便兩個類知道彼此的存在:

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:

#ifndef BAR_H
#define BAR_H

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

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

#ifndef FOO_H
#define FOO_H

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

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

請注意,兩個標頭不包含彼此.相反,他們只是通過前向聲明知道另一個類的存在.然后,在這兩個類的 .cpp 文件中,您可以#include 另一個標題來獲取有關該類的完整信息.這些前向聲明可以讓你打破foo需要bar需要foo需要bar"的引用循環.

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."

這篇關于如何在 C++ 中創建兩個相互用作數據的類?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 激情久久av一区av二区av三区 | 欧美99| 日韩av电影院 | 国产不卡在线观看 | 正在播放国产精品 | 国产视频精品视频 | 中文字幕一区二区三区精彩视频 | 欧洲一区在线观看 | 国产精品久久久久久福利一牛影视 | 九九热re| 中国一级特黄视频 | 午夜精品一区二区三区在线视频 | 伊人色综合久久久天天蜜桃 | 久久a久久 | 三级av在线 | 国产精品日产欧美久久久久 | 国产精品久久久久久久久久 | 欧美日韩精品在线免费观看 | 日韩中文字幕 | 欧美精品在线看 | 久久久99国产精品免费 | 综合五月婷 | 青青草av网站 | 男人的天堂久久 | 久久国产精品72免费观看 | 香蕉av免费 | h在线免费观看 | 欧美国产视频 | 欧美精品一区二区三 | 午夜精品一区二区三区在线观看 | 国产免费观看久久黄av片涩av | www.99re| 国产在线观看一区二区 | av永久免费| 在线观看第一区 | 国产高清精品一区二区三区 | 亚洲va欧美va人人爽午夜 | 亚洲第一av | 日本欧美在线视频 | 国产午夜精品视频 | 龙珠z国语版在线观看 |