开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜