【发布时间】:2017-12-15 16:24:01
【问题描述】:
有什么方法可以让derived class 访问base class 的private 成员,同时能够在@ 内的base class 方法上创建derived 对象987654326@?
类似这样的:
class Derived;
class Base
{
public:
void func()
{
// I want to create the derived obj here
Derived derived;
derived.func();
}
public:
int mBase = 5;
};
class Derived : public Base
{
public:
void func()
{
// I need to have access to the private members of Base inside this method
mBase = 6;
}
};
错误如下:
Error: derived uses undefined class Derived.
我该怎么做?
谢谢
【问题讨论】:
-
public: int mBase = 5;,根据您的问题,您可能是指private。事实上,您可能希望protected允许继承的类访问它。
标签: c++ inheritance derived-class base-class