tags:

views:

76

answers:

5

Would following the table below be the best way of determining the access type of member variables of a class that I'm creating (sorry if this table is hard to see; it's the same table shown 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
A: 

The C++ FAQs lite has more detail.

Matt Kane
+1  A: 

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

David Rodríguez - dribeas
That's funny ;-)
lothar
Not mine, though :)
David Rodríguez - dribeas
A: 

Your best bet is to learn the meaning of the public, protected, and private keywords. If the table helps you learn that then by all means use it; it is correct. If you want to be an effective C++ programmer this should be as easy as breathing.

Mark Ransom
+1  A: 

The table is correct, if that's what you're asking.

What it's saying in words is that you can always access member variables of the class your method is in. If the member variable is defined in a parent class then you can only access it if the member variable is protected or public. If you're outside the class then you can only access public member variables.

There is no "best way" -- these are the rules presented in a reasonable fashion.

Naaff
A: 

It's correct, aside from friends.

That being said, for any class that's more than a C-like struct of more or less related data, all member variables should be private. There is almost never a good reason to use protected variables.

David Thornley