C++对象引用作为对象的成员变量
我对 C++ 有点陌生,在设置主要架构时遇到了问题。我学会了如何在 C# 中使用这个特定的架构,但我无法让它在 C++ 中工作。 我的问题如下:我有 2 个对象。我希望这些对象彼此“了解”,因此我希望将这两个对象的引用作为其内部的成员变量。
考虑一下:
//main.cpp:
#include "Object1.h"
#include "Object2.h"
Object1 *obj1;
Object2 *obj2;
...
obj1 = new Object1(obj2);
obj2 = new Object2(obj1);
...
//Object1.h
#ifndef OBJECT1_H
#define OBJECT1_H
#include "Object2.h"
class Object1 {
public:
Object1(Object2*);
Object2 *obj;
}
#endif
//Object2.h
#ifndef OBJECT2_H
#define OBJECT2_H
#include "Object1.h"
class Object2 {
public:
Object2(Object1*);
Object1 *obj;
}
#endif
然而这是不可能的。因为如果没有 ifndef 的东西,你就会有这种无休止的迭代,包含彼此的 .h 文件。但是使用 ifndef 时,其中一个对象不会包含其他类定义,并且不知道应该创建什么对象。 这整个问题在 C# 中不会出现,因为您不必包含 .h 文件。你甚至没有 .h 文件:P。当您创建一个新类时,所有其他类都知道该类的存在。但在 C++ 中,您必须先包含特定类的 .h,然后才能创建该类的对象(甚至是引用)。
所以,我的问题是。如何创建两个相互引用的对象作为它们自己的成员变量?
感谢您的关注!
干杯,
马克西姆·舒梅克
I'm kinda new to C++ and I've run into a problem with setting up the main architecture. I learned how to use this specific architecture in C#, but I can't get it to work in C++.
My problem is as follows: I have 2 objects. I want these objects to 'know' of each other, so I want references of both these objects as member variables inside themselves.
Consider this:
//main.cpp:
#include "Object1.h"
#include "Object2.h"
Object1 *obj1;
Object2 *obj2;
...
obj1 = new Object1(obj2);
obj2 = new Object2(obj1);
...
//Object1.h
#ifndef OBJECT1_H
#define OBJECT1_H
#include "Object2.h"
class Object1 {
public:
Object1(Object2*);
Object2 *obj;
}
#endif
//Object2.h
#ifndef OBJECT2_H
#define OBJECT2_H
#include "Object1.h"
class Object2 {
public:
Object2(Object1*);
Object1 *obj;
}
#endif
This however is impossible. Because without the ifndef thingy you'd have this endless iteration of including eachothers .h files. But with the ifndef one of the objects doesn't get the others class definition included and doesn't know what object it's supposed to create.
This whole problem doesn't arise in C# because you don't have to include .h files. You don't even have .h files :P. When you make a new class every other class knows of this class's existence. But in C++ you have to include the .h of the specific class before it's possible to make an object of this class (or a reference even).
So, my question is. How do I make 2 objects with references to each other as member variables of themselves?
Thank you for your interest!
Cheers,
Maxim Schoemaker
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您使用预声明,即:
You use a pre-declaration, which is just:
为了处理您所描述的情况,在 c++ 中,有一种称为 -前向声明 的东西。
一些有用的链接:
http://en.wikipedia.org/wiki/Forward_declaration
http://www-subatech.in2p3.fr/ ~photons/subatech/soft/carnac/CPP-INC-1.shtml
To deal with the situation you described, in c++ , there is a thing called -Forward declaration.
A few useful links:
http://en.wikipedia.org/wiki/Forward_declaration
http://www-subatech.in2p3.fr/~photons/subatech/soft/carnac/CPP-INC-1.shtml
您不需要头文件中的
#include
。例如,在Object1.h
中,您可以只放置class Object2;
。这有很多优点。尽量避免在头文件中使用#include
,除非没有它们就无法编译。你可以做一些时髦的事情,比如
-
You do not need the
#include
in the header files. For example inObject1.h
you can just putclass Object2;
instead. This has lots of advantages. Try to avoid#include
in header files unless they do not compile without them.You can do funky things like
-
如果您仅使用指向 Objects1 的指针,则无需将 Object1.hpp 包含在 Object2.hpp 中。但是您需要将 Object1.hpp 包含在 Object2.cpp 中,因为它需要具有 Object1 的实际实现
You don't need to include Object1.hpp in Object2.hpp if you are only working with pointers to Objects1. But you need to include Object1.hpp in Object2.cpp since it needs to have the actual implementation of Object1