可以在派生类中修改私有基类成员变量吗?

-3 c++ inheritance

有没有办法将基类的私有成员变量设置为派生类的构造函数中的值?

我理解这是getter和setter方法的用途以及变量受保护或公共的用途,但假设您无法修改基类,是否有其他方法可以设置它?

Joh*_*136 6

不,这是私人的.这就是重点private.

从注释中的说明 - 基类确实为您提供了一种通过其构造函数来实现它的方法,因此您可以使用它.

// Assuming MyBaseclass has a 1 int constructor to set the
// private member, then something like this works.
//
MySubclass(int x) : MyBaseclass(x) {}
Run Code Online (Sandbox Code Playgroud)