Determining the Access Type of Class Member Variables

Will the following table best define the access type of the member variables of the class I'm creating (sorry if this table is hard to see, the same table shows http://www.cplusplus.com/doc/tutorial/inheritance/ )?

Access                        public    protected    private
members of the same class     yes       yes          yes
members of derived classes    yes       yes          no
not members                   yes       no           no

      

0


a source to share


4 answers


The table is correct if this is what you are asking for.

In words, you can always access the member variables of the class your method is in. If a member variable is defined in the parent class, you can only access it if the member variable is protected or public. If you are outside the class, you can only access public member variables.



There is no "best way" - these are rules reasonably presented.

+1


a source


You should add friend classes / methods there, but the friendship is clear: in C ++, friends can touch your private parts.



+1


a source


The best way is to learn the meaning of public, protected and private keywords. If a spreadsheet helps you know that then be sure to use it; right. If you want to be an effective C ++ programmer, it should be as easy as breathing.

0


a source


This is correct, apart from friends.

Moreover, for any class that is larger than a C-like structure of more or less related data, all member variables must be private. There has almost never been a reason to use protected variables.

0


a source







All Articles