开发者

How can private member variables in a superclass be accessed in a subclass?

开发者 https://www.devze.com 2023-01-07 13:09 出处:网络
I want to do something like: /* * Superclass.h * */ class Superclass { const int size; public: Superclass():size(1){}

I want to do something like:

/*
 * Superclass.h
 *
 */

class Superclass
{
const int size;

public:
    Superclass():size(1){}
    ~Superclass(){}
};


/*
 * 开发者_StackOverflowSubclass.h
 *
 */

#include "Superclass.h"

class Subclass : public Superclass
{

public:
    Subclass(){size;}
    ~Subclass(){}
};


Use protected instead of private


It seems that you need to access it only for construction. In that case, make a constructor which accepts the value:

Superclass(int size_value=1) : size(size_value) {}

Then use it in the subclass constructor, e.g.:

Subclass() : Superclass(5) {}
0

精彩评论

暂无评论...
验证码 换一张
取 消