How not to inherit a variable in C ++ classes

With C ++ classes, you can have a derived class that inherits a variable from its parent class. How to define a derived class so that var2 not inherited in derivclass ?

 class mainclass{ public: int var1; char var2; void test(){ cout<<var1<<var2<<endl; } } class derivclass : mainclass{ public: void test(){ cout<<var1<<var2<<endl; //want a compiler error here that var2 is not defined } } 
+7
source share
5 answers

The canonical way to prevent a member from inheriting in C ++ is to declare it private . Derived classes that try to access it then throw a compiler error. It will look like this:

 class mainclass{ public: int var1; void test(){ cout<<var1<<var2<<endl; } private: char var2; } class derivclass : mainclass { public: void test(){ cout<<var1<<var2<<endl; //compiler error here; var2 is not accessible } } 

This is the easiest way to achieve what you are asking for.

+7
source

You can make it private . But you probably shouldn't.

The fact that he should not inherit some things from Base means that he probably shouldn't inherit from Base at all.

Instead, create another base class and inherit two classes from this base.

 class Baseclass{ public: void test(){ cout<<var1<<endl; } protected: int var1; } class mainclass : public Baseclass{ public: char var2; void test(){ cout<<var1<<var2<<endl; } } class derivclass : Baseclass{ public: void test(){ cout<<var1<<endl; } } 
+4
source

As everyone says, you can make it private in the base class, and then it is not available in any subclasses. However, as @Dave says, it will still exist inside the base class. If you don’t even want it to exist as a variable hidden from subclasses, then you have to pull it out of the base class. Assuming a variable is needed for something, you will need to create a new subclass containing this variable.

+2
source

A derived class inherits each member of its foundations. Full stop. You cannot selectively inherit parts of a base class.

Creating a private variable does not prevent inheritance. The variable still exists, but under normal circumstances it is not available. With various friendship games you can make it accessible. Perhaps part of the confusion here is caused by Java, where private members are not inherited. I.e

 struct Base { private: int i; }; struct Derived : Base { }; Derived d; di = 3; // error: di is not accessible 

If i not inherited, the error would be that d does not have a member named i .

EDIT: another, perhaps more meaningful example

 void f(); class Base { void f(); }; class Derived : Base { void g() { f(); // error: Base::f is not accessible }; 

With Java rules, calling f() will be fine and will call global f() .

0
source

In response to all the other answers (and possibly switching to Stochastically), you can switch the main and derived class, where the new main class will have all the values ​​of the elements you want in the original derived class that are unnecessary in the new derived class (like the original main class)

Note:

 class newMainClass{ public: int var1; virtual void test(){ //added virtual here so there aren't two definitions for test() cout<<var1<<var2<<endl; //want a compiler error here that var2 is not defined } } class newDerivedClass : public newMainClass{ public: char var2; void test(){ cout<<var1<<var2<<endl; } } 
0
source

All Articles