Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

private - C++ access specifiers

I just want to make sure I got the idea of public and private right.

Regarding the private access specifier, does it mean:

  • Only accessed inside the class
  • Cannot be accessed from the object of the class unless there are public class methods that can be used to access them (Can other objects use those public functions?)
  • No other object can access them

And for public:

  • Accessed from the object of the class
  • Accessed from any other object

Is that right?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

private : Only member functions and friends of class can access it.
public : Can be accessed anywhere where the object has scope.


Answering the questions -

private:

  1. Yes
  2. Yes. (Can other objects use those public functions? With out having class relations, one object of class cannot communicate to the other's members. )
  3. Friends has access to private members of a class. So, answer depends upon your class has friends or not.

public:

  1. Yes
  2. Depends whether the object has hierarchical relations to the member's class you are trying to access.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...