调用父方法并访问父类中的私有变量? 我试图让下面的代码工作,但是我找不到足够的文档来说明C++如何处理公共与私有继承,以允许我做我想做的事情。如果有人能解释为什么我不能使用私有继承访问Parent::setSize(int)或Parent::size或使用公共继承访问Parent::size。要解决这个问题,我需要父级中的getSize()和setSize()方法 class Parent { private: int size; public: void setSize(int s); }; void Parent::setSize(int s) { size = s; } class Child : private Parent { private: int c; public: void print(); }; void Child::print() { cout << size << endl; } int main() { Child child; child.setSize(4); child.print(); return 0; } 类父类{ 私人: 整数大小; 公众: 无效设置大小(int s); }; void Parent::setSize(int s){ 尺寸=s; } 类子:私有父级{ 私人: INTC; 公众: 作废打印(); }; void子项::print(){ cout
使用私有继承时,基类的所有公共和受保护成员在派生类中都变为私有。在您的示例中,调用父方法并访问父类中的私有变量? 我试图让下面的代码工作,但是我找不到足够的文档来说明C++如何处理公共与私有继承,以允许我做我想做的事情。如果有人能解释为什么我不能使用私有继承访问Parent::setSize(int)或Parent::size或使用公共继承访问Parent::size。要解决这个问题,我需要父级中的getSize()和setSize()方法 class Parent { private: int size; public: void setSize(int s); }; void Parent::setSize(int s) { size = s; } class Child : private Parent { private: int c; public: void print(); }; void Child::print() { cout << size << endl; } int main() { Child child; child.setSize(4); child.print(); return 0; } 类父类{ 私人: 整数大小; 公众: 无效设置大小(int s); }; void Parent::setSize(int s){ 尺寸=s; } 类子:私有父级{ 私人: INTC; 公众: 作废打印(); }; void子项::print(){ cout,c++,inheritance,C++,Inheritance,使用私有继承时,基类的所有公共和受保护成员在派生类中都变为私有。在您的示例中,setSize在Child中变为私有,因此您不能从main调用它 另外,size在Parent中已经是私有的。一旦声明为私有,无论继承类型如何,成员始终对基类保持私有。您无法访问其他类的私有数据成员。如果您想访问超级las的私有属性,您应该通过公共或受保护的访问来访问教授。至于其余的,请参见@casablanca's。将家长更改为: protected: int size; #include <iostream
setSize
在Child
中变为私有,因此您不能从main
调用它
另外,
size
在Parent
中已经是私有的。一旦声明为私有,无论继承类型如何,成员始终对基类保持私有。您无法访问其他类的私有数据成员。如果您想访问超级las的私有属性,您应该通过公共或受保护的访问来访问教授。至于其余的,请参见@casablanca's。将家长更改为:
protected: int size;
#include <iostream>
using namespace std;
class Parent {
private:
int size;
public:
void setSize(int s);
int fun() //member function from base class to access the protected variable
{
return size;
}
};
void Parent::setSize(int s) {
size = s;
}
class Child : private Parent {
private:
int c;
public:
void print();
using Parent::setSize; //new code line
using Parent::fun; //new code line
};
void Child::print() {
cout << fun() << endl;
}
int main() {
Child child;
child.setSize(4);
child.print();
return 0;
}
如果要从派生类而不是从类外部访问size
成员,则需要受保护
将子项更改为:
class Child: public Parent
当你说class Child:private Parent
时,你是说孩子是父母应该是一个秘密。你的main
代码清楚地表明你希望孩子作为父母被操纵,所以它应该是公共继承。\35; include
#include <iostream>
using namespace std;
class Parent {
private:
int size;
public:
void setSize(int s);
int fun() //member function from base class to access the protected variable
{
return size;
}
};
void Parent::setSize(int s) {
size = s;
}
class Child : private Parent {
private:
int c;
public:
void print();
using Parent::setSize; //new code line
using Parent::fun; //new code line
};
void Child::print() {
cout << fun() << endl;
}
int main() {
Child child;
child.setSize(4);
child.print();
return 0;
}
使用名称空间std;
班级家长{
私人:
整数大小;
公众:
无效设置大小(int s);
int fun()//访问受保护变量的基类的成员函数
{
返回大小;
}
};
void Parent::setSize(int s){
尺寸=s;
}
类子:私有父级{
私人:
INTC;
公众:
作废打印();
使用父::setSize;//新代码行
使用Parent::fun;//新代码行
};
void子项::print(){
是的,私有成员不是继承的,这就是为什么不能访问基的私有成员class@NedBatchelder:这就是我投票支持你的答案的原因。:)我不确定OP到底想要什么,因为否则正常的公共继承会很好。欢迎使用堆栈溢出!我推荐你。当给出答案时,它是pr请解释一下为什么你的答案是正确的。