Is there an easy way to use base class variables?

When you have a derived class, is there an easier way to refer to a variable from a method other than:

BaseClass::variable

EDIT
How this happened, I found a page that explained this problem using functions: Errors generated by templates . Apparently this matters when using the template classes.

+3
source share
2 answers

If the member variable of the base class is protected or public, you can simply reference it by name in any member function of the derived class. If it is private to the base class, the compiler will not allow the derived class to access it at all. Example:


class Base
{
protected:
  int a;

private:
  int b;
};

class Derived : public Base
{
  void foo()
  {
    a = 5;  // works
    b = 10; // error!
  }
};

, - .

, "" :


class Base
{
public:
  int a;
};

class Derived : public Base
{
public:
  int a;
};

a: Base, Derived, , , .

+10
+1

Source: https://habr.com/ru/post/1698761/


All Articles