开发者

In C++ how to have memory only for one variable define both in base & derived class

Is it possible to have memory for only one variable (defined in both base as well as derived class)if we create object of derived class. If can, what is the reason behind it, please provide complete explanation.

开发者_开发问答
class Base
{
private:
    int i;
}

class derived: public /* or private or protected (whatever fits in my question) */
    Base
{
private:
    int i;
}

int main()
{
   derived obj;
}


If you have a variable in a parent class, you shouldn't need to declare it in the child class. That's part of what it means to be derived; it inherits all of the members of the parent, including any variables. It's only going to be using one spot in memory, and it's only defined once: in the parent. You'll just need to make sure it's defined as protected or public in the parent.


What you really want is to access the same member in both mother and child classes :

    class Base { protected: int i; }; // allow child class only to access i

class derived: public Base {  
    private:
    void blah() { i = 42; } // valid, we access a protectd 
};

I used public inheritance but it would still work with other inheritance.


From your comments to the other answers I learnded, that you don't want to access Base::i and you want that it don't even exist.

So the answer is: No, this is not possible.

There are various reasons for this: Memory Layout would be much more complicated, Polymorphism wouldn't work fine, also "features" like "slicing" would break.

Why do you have this requirement? In the example you use an int. Do you have a real world problem where a much larger object is the member variable? If so, you could use a pointer and create the object dynamically, if it is needed. (Use a public constructor and a protected constructor in the base class: The public one to create a base class object that then also creates your member variable with new and the protected one to be called from derived class' constructors).

Anyhow: Propably something is wrong with your class design: Inheritance means a "is a" association, so usually all "properties" (=member variables) of your base class, are also part of the derived classes.


You can declare the field in the base class as protected, and it will be accessible from derived classes.

However, protected fields are generally frowned upon in the C++ world¹. It is probably better to make the field private, and provide protected accessor methods (get_i() and set_i() or some such) instead.


¹ According to this, Bjarne Stroustrup himself wrote:

Members declared protectedare far more open to abuse than members declared private . In particular, declaring data members protected is usually a design error. Placing significant amounts of data in a common class for all derived classes to use leaves that data open to corruption. Worse, protected data, like public data, cannot easily be restructured because there is no good way of finding every use. Thus, protected data becomes a software maintenance problem.

The C++ FAQ has a more nuanced opinion, or rather, explains when protected fields might be a good idea.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜