tags:

views:

92

answers:

4

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;
}
+2  A: 

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.

Klaim
I don't want to access the base member. I want if derived member is created with same name and type, then base member of same name and type should not get memory or come in to existence if I create an object of derived class. Is it possible?
decisive
However, providing protected methods instead of protected fields would be preferable for information hiding.
Andy Thomas-Cramer
+3  A: 

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.

Alex Zylman
I don't want to access the base member. I want if derived member is created with same name and type, then base member of same name and type should not get memory or come in to existence if I create an object of derived class.
decisive
How could that possibly work? Base class methods have to be able to access all the members of the base class, regardless of whether their this-pointer references an instance of the derived class.
Zack
@decisive: First, what you are asking is not possible. The base class will be fully constructed regardless of whether it's part of a derived or not. Consider that, if you assign a derived-class object to a base-class object variable, you slice off the derived portions and get a base-class object. Second, I have no idea why you'd want to do that.
David Thornley
A: 

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.

Thomas
Why are `protected` fields frowned upon? Having protected getter/setter methods for fields with simple datatypes has always seemed to me like a rather pointless thing to do, considering the end result is pretty much identical whether you set/read the field directly from the derived class or invoke the getter/setter methods. It _does_ make sense when you need to perform validation or object creation of course...
Chinmay Kanchi
I tried to find a reference for this assertion, but did not try very hard. Anyway: the protected part of a class serves as the interface to derived classes. Making a field protected means that you are mixing interface and implementation. For example, if you change the field's type or representation, this will require changes to all derived classes. (And in the resulting compiler output, due to inlining, the end result is *exactly* identical to directly using the field.)
Thomas
@Chinmay: `protected` fields are frowned upon because they indicate there isn't a clear separation between the base and derived classes. The coupling they introduce makes it difficult to change the implementation of the base class and enforce invariants. Accessors aren't much of an improvement, and still indicate confusion about the roles of the classes, but they do provide some separation between the interface and the implementation of the base class.
Mike Seymour
@Chinmay: I guess it's the argument that you might, in future, want to perform validation, or change the implementation so that the abstract object state currently represented by that field, is represented differently. C++ base classes are fragile, and protected accessors don't let you hide the class layout in a DLL the way public accessors can. But you might still plan for source compatibility for subclasses, which means you might want to be able to "hook" any operation which accesses the base class. Hence, no protected or public fields.
Steve Jessop
I've found the reference I was looking for and added a footnote to my answer.
Thomas
A: 

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.

IanH