对象 A 中的对象 B 和对象 B 中对象 A 的引用,没有指针
Object B in object A and reference of object A in object B without pointers
所以我想做的是存储:
- 对象 A 中的对象 B 和
- 对象 B 中对象 A 的引用
同时不使用指针。
我尽量避免使用指针和引用之间的唯一区别是访问语法。我不想每次访问对象 B 中的对象 A 时都写'->'。
我认为可以工作但抛出分段错误的代码:
A.h
#ifndef A_H
#define A_H
class B;
class A{
B b;
public:
A();
};
#endif
B.h
#ifndef B_H
#define B_H
class A;
class B{
A& a;
public:
B(A &_a);
};
#endif
A.cpp
#include "A.h"
#include "B.h"
A::A():b(B(*this)){}
B.cpp
#include "B.h"
#include "A.h"
B::B(B &_b):a(_b){}
我认为导致分段错误的第一件事是在初始化列表中使用 'this' 关键字(未初始化的实例),但我已经读过,只要我不访问它,一切都应该没问题.我的构造函数是空的,所以我不知道有什么问题。
是否可以按照我的方式进行操作?如果没有,那为什么,有什么东西可以让我不写'->'吗?
编辑:
确实有一些编译错误,因为它只是作为伪代码编写的,只是为了不在此处粘贴不必要的代码,所以没有人会浪费时间。编写伪代码后当然可以编译。 咕.gl/DHlM6X
但现在它运行时没有段错误。我想我在我的项目中做了一些不同的事情。我将不得不测试一段时间为什么它在项目中不起作用,我将 post 问题是什么,以便问题可以有真正的答案。
乍一看,在这里使用引用而不是乱用指针似乎是个好主意,但您对此无能为力。为什么?引用是指 link 对象,您可以确定 linked 对象的寿命比引用长。
所以你的代码
class B{
A& a;
};
基本上表示:"For every object of type B
there is a known, not changeable object of type A
that exists before the object of type B
is created and will still live at the time the B
-object is destructed"。
同时你的代码
class A{
B b;
};
状态:"Every object of type A
consists of an object of type B
",它基本上告诉编译器为类型 A
的对象分配内存(包括 B b
的 space),然后构造一个B
类型的对象在该内存中的正确位置,然后构造一个 A
类型的对象。但是在这个例子中,A 是在 after B 中构造的,在 before B.
中被销毁了
两种说法相互排斥。
所以要么你切换到指针(状态 "The object knows about another object of type X
" 并且可以是 nullptr
)或者你可以考虑做这样的事情:
class Combined{
A a;
B b;
}
然后在 Combined
class.
中添加需要了解两个对象的函数
在标准中,我发现了以下关于您的问题以及为什么它根本不起作用的原因:
9.3.2 The this pointer [class.this]
In the body of a non-static (9.3) member function, the keyword this
is a prvalue expression whose value is the address of the object for which the function is called. The type of this in a member function of
a class X is X*. If the member function is declared const(...)
所以据我所知,this
只定义了行为 "inside the body of a non-static member function"。一些编译器在 class 的其他部分也允许 this
、typeid(this)
或 sizeof(this)
,但我在标准中找不到这个。
所以我想做的是存储:
- 对象 A 中的对象 B 和
- 对象 B 中对象 A 的引用
同时不使用指针。
我尽量避免使用指针和引用之间的唯一区别是访问语法。我不想每次访问对象 B 中的对象 A 时都写'->'。
我认为可以工作但抛出分段错误的代码:
A.h
#ifndef A_H
#define A_H
class B;
class A{
B b;
public:
A();
};
#endif
B.h
#ifndef B_H
#define B_H
class A;
class B{
A& a;
public:
B(A &_a);
};
#endif
A.cpp
#include "A.h"
#include "B.h"
A::A():b(B(*this)){}
B.cpp
#include "B.h"
#include "A.h"
B::B(B &_b):a(_b){}
我认为导致分段错误的第一件事是在初始化列表中使用 'this' 关键字(未初始化的实例),但我已经读过,只要我不访问它,一切都应该没问题.我的构造函数是空的,所以我不知道有什么问题。
是否可以按照我的方式进行操作?如果没有,那为什么,有什么东西可以让我不写'->'吗?
编辑:
确实有一些编译错误,因为它只是作为伪代码编写的,只是为了不在此处粘贴不必要的代码,所以没有人会浪费时间。编写伪代码后当然可以编译。
但现在它运行时没有段错误。我想我在我的项目中做了一些不同的事情。我将不得不测试一段时间为什么它在项目中不起作用,我将 post 问题是什么,以便问题可以有真正的答案。
乍一看,在这里使用引用而不是乱用指针似乎是个好主意,但您对此无能为力。为什么?引用是指 link 对象,您可以确定 linked 对象的寿命比引用长。
所以你的代码
class B{
A& a;
};
基本上表示:"For every object of type B
there is a known, not changeable object of type A
that exists before the object of type B
is created and will still live at the time the B
-object is destructed"。
同时你的代码
class A{
B b;
};
状态:"Every object of type A
consists of an object of type B
",它基本上告诉编译器为类型 A
的对象分配内存(包括 B b
的 space),然后构造一个B
类型的对象在该内存中的正确位置,然后构造一个 A
类型的对象。但是在这个例子中,A 是在 after B 中构造的,在 before B.
两种说法相互排斥。
所以要么你切换到指针(状态 "The object knows about another object of type X
" 并且可以是 nullptr
)或者你可以考虑做这样的事情:
class Combined{
A a;
B b;
}
然后在 Combined
class.
在标准中,我发现了以下关于您的问题以及为什么它根本不起作用的原因:
9.3.2 The this pointer [class.this]
In the body of a non-static (9.3) member function, the keyword this is a prvalue expression whose value is the address of the object for which the function is called. The type of this in a member function of a class X is X*. If the member function is declared const(...)
所以据我所知,this
只定义了行为 "inside the body of a non-static member function"。一些编译器在 class 的其他部分也允许 this
、typeid(this)
或 sizeof(this)
,但我在标准中找不到这个。