0

The Google C++ style guide states in the section about inheritance that:

Limit the use of protected to those member functions that might need to be accessed from subclasses. Note that data members should be private.

Consider now the following case:

class A {
    private:
         double m;
    // ...
}

class B : public A {
    // ...
}

I guess the correct approach is that class A implements getter/setter functions for m such that class B can access it?

I don't really get why this rule has been chosen instead of using protected for data members as well. Could someone explain?

user2416984
  • 941
  • 1
  • 11
  • 18
  • 4
    It's probably off-topic because primarily opinion based, but [this guy](https://www.youtube.com/watch?v=NOCElcMcFik) probably has the most relevant opinion in this case and explains why you should not follow the google style guide. – nwp Feb 02 '18 at 15:21
  • dont confuse what the google style guide suggests with "the correct approach". What might be appropriate for a millions lines code base with lots of old code is not necessarily the best for your project – 463035818_is_not_an_ai Feb 02 '18 at 15:21
  • 1
    Adding getters and setters defeats the purpose of hiding the representation. The "correct" approach is to design `A` so that `B` doesn't need to be aware that `m` exists. – molbdnilo Feb 02 '18 at 16:42

0 Answers0