C++:为什么我的 DerivedClass 的构造函数不能访问 BaseClass 的受保护字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3319892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: Why does my DerivedClass's constructor not have access to the BaseClass's protected field?
提问by dwinchell
I have a constructor attempting to initialize a field in a base class. The compiler complains. The field is protected, so derived classes should have access.
我有一个构造函数试图初始化基类中的一个字段。编译器抱怨。该字段是受保护的,因此派生类应该可以访问。
//The base class:
class BaseClass
{
public:
BaseClass(std::string);
BaseClass(const BaseClass& orig);
virtual ~BaseClass();
const std::string GetData() const;
void SetData(const std::string& data);
protected:
BaseClass();
std::string m_data;
};
BaseClass::BaseClass(const std::string data) : m_data(data) { }
BaseClass::BaseClass() { }
BaseClass::BaseClass(const BaseClass& orig) { }
BaseClass::~BaseClass() { }
void BaseClass::SetData(const std::string& data)
{
m_data = data;
}
const std::string BaseClass::GetData() const
{
return m_data;
}
//The derived class:
class DerivedClass : public BaseClass
{
public:
DerivedClass(std::string data);
DerivedClass(const DerivedClass& orig);
virtual ~DerivedClass();
private:
};
DerivedClass::DerivedClass(std::string data) : m_data(data) { } //ERROR HERE
DerivedClass::DerivedClass(const DerivedClass& orig) { }
DerivedClass::~DerivedClass() { }
//The compiler error
//编译错误
DerivedClass.cpp:3: error: class ‘DerivedClass' does not have any field named ‘m_data'
DerivedClass.cpp:3: 错误:类“DerivedClass”没有任何名为“m_data”的字段
Any help is greatly appreciated. Thank you in advance.
任何帮助是极大的赞赏。先感谢您。
回答by Zitrax
You cannot initialize m_data in the derived class constructor but instead pass it as an argument to the base class constructor.
您不能在派生类构造函数中初始化 m_data,而是将其作为参数传递给基类构造函数。
That is: DerivedClass::DerivedClass(std::string data) : BaseClass(data) { }
那是: DerivedClass::DerivedClass(std::string data) : BaseClass(data) { }
回答by neves
In the initializer list you can just set values for attributes of the same class. To access it, you must attribute the value in the body of the constructor:
在初始化列表中,您可以只为同一类的属性设置值。要访问它,您必须在构造函数的主体中属性值:
DerivedClass::DerivedClass(std::string data) {m_data = data; }
DerivedClass::DerivedClass(std::string data) {m_data = data; }
Or, if it is expensive to copy the object, you pass the m_data as an argument to the parent class constructor :
或者,如果复制对象的成本很高,则将 m_data 作为参数传递给父类构造函数:
DerivedClass::DerivedClass(std::string data) : BaseClass(data) {}
DerivedClass::DerivedClass(std::string data) : BaseClass(data) {}
Tip: Pass your data as a reference to prevent the copy constructor.
提示:将您的数据作为引用传递以防止复制构造函数。
See more info here: order of initialization of C++ constructors.
在此处查看更多信息:C++ 构造函数的初始化顺序。
回答by James Curran
You are not "accessing" m_data -- you are initializing it. However, it's already been initialized in the Base Class's ctor. If you want to change it's value, assign to it in the body of your ctor:
您不是在“访问” m_data - 您正在初始化它。但是,它已经在基类的构造函数中初始化。如果要更改它的值,请在 ctor 的主体中分配给它:
DerivedClass::DerivedClass(std::string data)
{
m_data = data;
}
回答by code-gijoe
You need to call the base class constructor as follows:
您需要按如下方式调用基类构造函数:
DerivedClass::DerivedClass(std::string data) : BaseClass(data) {
}
Each class should be in charge of initializing it's members.
每个类都应该负责初始化它的成员。
回答by JaredPar
Initializer lists can only be used to initialize fields which are owned by the type in question. It is not legal to initialize base class fields in an initializer lists which is why you receive this error. The field is otherwise accessible within DerivedClass
初始值设定项列表只能用于初始化相关类型拥有的字段。在初始化列表中初始化基类字段是不合法的,这就是您收到此错误的原因。该字段可在DerivedClass
回答by Will Manley
Your derived class constructor does have access, but you cannot assign to it in the initialisation list. Change the constructor to:
您的派生类构造函数确实有权访问,但您不能在初始化列表中分配给它。将构造函数更改为:
DerivedClass::DerivedClass(const std::string& data)
: BaseClass(data)
{
}
Alternatively, if no suitable base class constructor is available and you cant add one you can change the constructor to:
或者,如果没有合适的基类构造函数可用并且您无法添加一个,则可以将构造函数更改为:
DerivedClass::DerivedClass(const std::string& data)
{
m_data = data;
}