gpt4 book ai didi

c++ - 无法从子实例调用基类的公共(public)方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:04:57 26 4
gpt4 key购买 nike

我想在 C++ 中尝试构造函数继承,它运行良好。但是后来我发现我不能真正从Daughter class 的实例中调用方法。 Visual Studio 说

the method Mother::ShowName is not available

即使它是公开的,但就我而言,它必须可以从子类(class)获得。我做错了什么吗?

class Mother{

protected:
char* name;

public :
Mother(char* _name){
name = _name;
}

void ShowName(){
cout << "my name is: " << name << endl;
}
};


class Daughter : Mother{
public:
Daughter(char* _name) : Mother(_name) {
}
};

int main(){
Daughter d1("Masha");
d1.ShowName();

return 0;
}

最佳答案

class Daughter : Motherprivate遗产。 class默认情况下继承就是这样。

class Daughter : public Mother正是您要找的。

关于c++ - 无法从子实例调用基类的公共(public)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16341934/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com