C++循环依赖:构造函数应该是什么样子?
我阅读了很多循环依赖主题,但它们似乎都与声明有关。我感兴趣的是如何构建相互依赖的对象以及我的方法是否存在潜在的陷阱。考虑这个简单的例子:
#include <iostream>
#include <vector>
using namespace std;
class A; //Forward declaration
class B{
public:
B(string name, A* a):myA(a), name(name){
cout << "Works with pointer" << endl;
};
private:
A* myA;
string name;
};
class A{
public:
A(){
cout << "Constructing A" << endl;
if(bs.empty()) cout << "Vector is empty" << endl;
bs.push_back(B("First", this));
cout << "Array has " << bs.size() << " elements." << endl;
};
private:
std::vector<B> bs;
};
int main() {
cout << "Start" << endl;
A a;
cout << "Ok." << endl;
return 0;
}
我可以做些什么来避免 B
中的 A*
指针吗?
理想情况下,我希望有一个参考,但是如果我将 B
的构造函数更改为 B(string name, A& a)
,然后将 Push_back 更改为 bs.bush_back(B("First", *this));
我收到错误:非静态引用成员 'A& B::myA',不能使用默认赋值运算符
。 查看修改后的示例
据我所知,合成的 operator=
编译器不适合这里。正确的 operator=
是什么样子的?还是我一起走错了方向?
I read through a lot of circular dependency topics but all of them seem to be concerned with the declaration. I am interested how to construct the mutually depended objects and if there are potential pitfalls with my approach. Consider this simple example:
#include <iostream>
#include <vector>
using namespace std;
class A; //Forward declaration
class B{
public:
B(string name, A* a):myA(a), name(name){
cout << "Works with pointer" << endl;
};
private:
A* myA;
string name;
};
class A{
public:
A(){
cout << "Constructing A" << endl;
if(bs.empty()) cout << "Vector is empty" << endl;
bs.push_back(B("First", this));
cout << "Array has " << bs.size() << " elements." << endl;
};
private:
std::vector<B> bs;
};
int main() {
cout << "Start" << endl;
A a;
cout << "Ok." << endl;
return 0;
}
Is there anything that I could do to avoid the A*
pointer in B
?
Ideally I would like to have a reference, but if I change the constructor of B
to B(string name, A& a)
and then change the push_back to bs.bush_back(B("First", *this));
I get an error: non-static reference member 'A& B::myA', can't use default assignment operator
. See the modified example
As far as I can tell the operator=
that is synthesized by the compiler does not fit here. How would the proper operator=
look like? Or am I going the wrong direction all together?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您在标准容器中使用类型时,例如
vector
,该类型需要是CopyConstructible 和Assignable。如果您的类型具有指针成员,那么这些成员可以与隐式定义的复制赋值运算符一起正常工作,但引用成员的情况并非如此,因为引用无法反弹。如果您希望您的类型在容器中工作,那么坚持使用指针成员要简单得多。是否可以定义一个在您的特定情况下有意义的复制赋值运算符并不明显,通常情况下是没有意义的。
When you use a type in a standard container, like
vector
the type needs to be CopyConstructible and Assignable. If your type has pointer members then these will work fine with an implicitly defined copy assignment operator but this is not the case with reference members as references can't be rebound.If you want your type to work in a container it is much simpler to stick with pointer members. It's not obvious whether you can define a copy assignment operator that makes sense in your particular case, in general it isn't.
即使使用用户定义的赋值运算符,也不能在那里使用引用,因为引用一旦初始化就无法重新绑定。这里使用指针是最简单的方法。
You couldn't use a reference there even with a user-defined assignment-operator, because references cannot be rebound once they are initialized. Using a pointer here is the simplest approach.
在这种情况下(并且仅在这种情况下),您可以考虑将
B
制作为模板。这个建议纯粹没有任何上下文...
In this case (and only in this case), you could consider making
B
a template..This suggestion is purely without any context...