struct和class相互继承时,默认的继承方式
基类是struct,派生类也是struct,默认的继承方式是public的;
基类是class,派生类也是class,默认的继承方式是private的。
struct和class默认的继承关系如下:
(1).基类是struct,派生类是class,默认的继承方式是private的
struct base
{
int a;
};
class derived : base
{
};
int main(int argc, char* argv[])
{
derived d;
d.a = 0; /* error: 成员a不可访问 */
return 0;
}
(2).基类是class,派生类是struct,默认的继承方式是public的
class base
{
public:
int a;
};
struct derived: base
{
int b;
};
int main(int argc, char* argv[])
{
struct derived d;
d.a = 0; /* ok: 成员a可访问 */
return 0;
}
END