[英]Initializing class object inside the other class constructor
I have a class, it names A and A class has 3 another class in its private. 我有一个班级,它的名字叫A,而一个班级有3个私人班级。
class A{
public:
A();
A(int num);
A(C& mC, P& mP, M& mM, int num);
//there is a getter and setter for all member this only one example(please check are they right?)
M getM()const{return pM;}
void setM(M classM){ pM = classM ;}
private:
C& pC;
P& pP;
M& pM;
int digit= 0;
};
I'm doing that in parameter constucture: 我在参数结构中这样做:
A::A(C& mC, P& mP, M& mM, int num):pC(mc),pP(mP),pM(mM)
{
// doing someting here
}
But I can't write a code for default and first parameter constructure, when I write something compiler saying to me that : 但是,当我写一些对我说的编译器时,我无法为默认和第一个参数构造编写代码:
error: uninitialized reference member in 'class A&' [-fpermissive] A::A(){ 错误:“类A&”中未初始化的引用成员[-fpermissive] A :: A(){
and 和
note: 'A& A::pP' should be initialized A& pP; 注意:“ A&A :: pP”应初始化为A&pP;
someting like this, several errors and notes. 这样的东西,一些错误和注释。
What should I do? 我该怎么办? How can I initialize classes in default and first parameter constructure? 如何在默认和第一个参数构造中初始化类?
Class A
contains references to other object. 类A
包含对其他对象的引用 。 Unlike pointers, references cannot be null. 与指针不同,引用不能为空。 To make this work, you either need to: 要使其工作,您要么需要:
nullptr
is no valid object is provided in the constructor 使用指针代替引用,并将其初始化为nullptr
,因为构造函数中未提供有效的对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.