简体   繁体   English

在派生中创建构造函数时 class

[英]When creating a constructor in a derived class

When creating a constructor in a derived class, Using the base class's private member variable, Can it be given as an initial value for a derived class's constructor parameter? class 在派生类中创建构造函数时,使用基类的私有成员变量,是否可以作为派生类构造函数参数的初始值?

class Base{
    private: int m_Par;
    protected: int m_Pro;
    public:int m_Pub;
};
class Derived :public Base{
    public:Derived(int m_Par=1,int m_Pro=2,int m_Pub=3);
           void Func();
};
Derived::Derived(int m_Par,int m_Pro,int m_Pub)
{
    //this->m_Par=m_Par;
    this->m_Pro=m_Pro;
    this->m_Pub=m_Pub;
}

I remember that derived classes can't access the private member variables of the base class, However, I wonder why there is no error when the private member variable of the base function is given as the initial value of the derived class parameter.我记得派生类是不能访问基类class的私有成员变量的,但是我想知道为什么把基类function的私有成员变量作为派生类class参数的初始值时没有报错。

are you compiling with the line commented out?你在编译注释掉的行吗? because when i compile with g++ the compiler complaints with the error below.因为当我用 g++ 编译时,编译器抱怨以下错误。

derived.cpp: In constructor ‘Derived::Derived(int, int, int)’:
derived.cpp:12:11: error: ‘int Base::m_Par’ is private within this context
this->m_Par=m_Par;
       ^~~~~
derived.cpp:2:18: note: declared private here
private: int m_Par;

Parameter is not the field.参数不是字段。 When you write parameter names to your derived classes constructor like Derived(int m_Par=1,int m_Pro=2,int m_Pub=3) it doesn't match with the fields.当您将参数名称写入派生类构造函数时,如Derived(int m_Par=1,int m_Pro=2,int m_Pub=3)它与字段不匹配。 You are matching fields with the parameters in the function body if you want so.如果需要,您可以将字段与 function 正文中的参数匹配。

If you want to initialize a private field in base class you can use something like that.如果你想在 base class 中初始化一个私有字段,你可以使用类似的东西。

class Base{
    private: int m_Par;
    protected: int m_Pro;
    public:int m_Pub;

    Base(int m_Par){
        this->m_Par = m_Par;
    }

    int getM_Par(){
        return this->m_Par;
    }
};
class Derived :public Base{
    public:Derived(int m_Par=1,int m_Pro=2,int m_Pub=3);
           void Func();
};
Derived::Derived(int m_Par,int m_Pro,int m_Pub) : Base(m_Par)
{
    this->m_Pro=m_Pro;
    this->m_Pub=m_Pub;
}

int main(){
   Derived d(10,5,3);
   std::cout<<"M_Par value: "<<d.getM_Par();
   return 0;
}

EDIT: In the code below you wrote this->m_Pro = m_Pro;编辑:在下面的代码中你写了this->m_Pro = m_Pro; and this->m_Pub=m_Pub;this->m_Pub=m_Pub; . . Those code blocks assigns the values taken from parameters to your base classes fields.这些代码块将从参数中获取的值分配给您的基类字段。 When you write Derived:: Derived(int m_Par =1, int m_Pro=2, int m_Pub=3) what you are doing is that if user doesn't pass any parameters to the function your parameters will have default values that you have assigned.当您编写Derived:: Derived(int m_Par =1, int m_Pro=2, int m_Pub=3)时,您正在做的是,如果用户未将任何参数传递给 function,您的参数将具有您分配的默认值. So parameters and the fields are not the same.所以参数和字段是不一样的。 The fields you wrote in the base class are belong to base class but parameters are local variables belong to the function you passed into.你在base class中写的字段属于base class但是参数是局部变量属于你传入的function。

Derived::Derived(int m_Par,int m_Pro,int m_Pub) : Base(m_Par)
{
    this->m_Pro=m_Pro;
    this->m_Pub=m_Pub;
}

First of all, m_Par is the local variable in your constructor and has nothing to do with the member m_Par of the Base class. In order to access the member with the same name as the local variable use this->m_Par .首先, m_Par是构造函数中的局部变量,与Base class 的成员m_Par无关。为了访问与局部变量同名的成员,请使用this->m_Par However, You can't access private members of the Base class in the constructor of your Derived class.但是,您不能在Derived class 的构造函数中访问Base class 的private成员。

I think that you just complicated your question when mixed naming convention for local variables and class members .我认为当混合local variablesclass members的命名约定时,您只是使问题复杂化了。 Prefix m_ is usually added to the names of class members:前缀m_通常添加到 class 成员的名称中:

Base(int par){
    this->m_Par = par;
}

Derived::Derived(int par,int pro,int pub) : Base(par)
{
    this->m_Pro = pro;
    this->m_Pub = pub;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM