使用类声明中的参数启动类
在我的对象状态中,我想要对象 pq。但 pq 需要用参数初始化。有没有办法将依赖于参数的类包含到另一个类中?
文件.h
Pq 类 { 整数a; Pq(B类b); }; 类状态 { B级b2; Pq pq(b2); 状态(B类b3); };
文件.cc
状态::状态(ClassB b3) : b2(b3) {}
in my object State, i'd like to have object pq. but pq needs to be initialized with a parameter. is there a way to include a class that depends on a parameter into another class?
file.h
class Pq { int a; Pq(ClassB b); }; class State { ClassB b2; Pq pq(b2); State(ClassB b3); };
file.cc
State::State(ClassB b3) : b2(b3) {}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在初始化程序列表中初始化它,就像使用
b2
一样:请记住,成员按照声明的顺序进行初始化在头文件中,而不是初始值设定项列表中初始值设定项的顺序。
您还需要删除标头中初始化它的尝试:
You can initialize it in the initializer list, just like you do with
b2
:Keep in mind that members are initialized in the order they are declared in the header file, not the order of the initializers in the initializer list.
You need to remove the attempt at initialize it in the header as well:
在上面的代码中,您必须在初始化列表中维护该顺序,否则您将得到意想不到的结果。因此风险很大
In the above code you have to maintain that order in the initialization list otherwise you will get what not expected..therefore quite risky