C++:为什么我的 DerivedClass 构造函数无法访问 BaseClass 的受保护字段?
我有一个构造函数尝试初始化基类中的字段。编译器抱怨。该字段受保护,因此派生类应该具有访问权限。
//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() { }
//编译器错误
DerivedClass.cpp:3: 错误: 类 'DerivedClass' 没有任何名为 'm_data' 的字段
非常感谢任何帮助。先感谢您。
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’
Any help is greatly appreciated. Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您不能在派生类构造函数中初始化 m_data,而是将其作为参数传递给基类构造函数。
即:DerivedClass::DerivedClass(std::string data) : BaseClass(data) { }
You cannot initialize m_data in the derived class constructor but instead pass it as an argument to the base class constructor.
That is:
DerivedClass::DerivedClass(std::string data) : BaseClass(data) { }
在初始值设定项列表中,您只需为同一类的属性设置值。要访问它,您必须在构造函数主体中赋予该值:
,如果复制对象的成本很高,则可以将 m_data 作为参数传递给父类构造函数:
提示:将数据作为引用传递以防止复制构造函数。
请在此处查看更多信息:C++ 构造函数的初始化顺序。
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:
Or, if it is expensive to copy the object, you pass the m_data as an argument to the parent class constructor :
Tip: Pass your data as a reference to prevent the copy constructor.
See more info here: order of initialization of C++ constructors.
你不是在“访问”m_data——你是在初始化它。但是,它已经在基类的构造函数中进行了初始化。如果您想更改它的值,请在您的 ctor 主体中分配给它:
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:
您需要按如下方式调用基类构造函数:
每个类都应负责初始化其成员。
You need to call the base class constructor as follows:
Each class should be in charge of initializing it's members.
初始化列表只能用于初始化相关类型所拥有的字段。在初始化器列表中初始化基类字段是不合法的,这就是您收到此错误的原因。该字段可以在
DerivedClass
中访问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
您的派生类构造函数确实具有访问权限,但您无法在初始化列表中分配给它。将构造函数更改为:
或者,如果没有合适的基类构造函数可用并且您无法添加一个,则可以将构造函数更改为:
Your derived class constructor does have access, but you cannot assign to it in the initialisation list. Change the constructor to:
Alternatively, if no suitable base class constructor is available and you cant add one you can change the constructor to: