Chinaunix

标题: 私有成员可以这样访问? [打印本页]

作者: aaaaal    时间: 2009-07-28 13:50
标题: 私有成员可以这样访问?
class MyString {
 private:
  const char * c_string_;
  const MyString& operator=(const MyString& rhs);

 public:

  // Clones a 0-terminated C string, allocating memory using new.

  static const char * CloneCString(const char * c_string);

  ////////////////////////////////////////////////////////////

  //

  // C'tors


  // The default c'tor constructs a NULL string.

  MyString() : c_string_(NULL) {}

  // Constructs a MyString by cloning a 0-terminated C string.

  explicit MyString(const char * c_string) : c_string_(NULL) {
    Set(c_string);
  }

  // Copy c'tor

  MyString(const MyString& string) : c_string_(NULL) {
    Set(string.c_string_);
  }

  ////////////////////////////////////////////////////////////

  //

  // D'tor.  MyString is intended to be a final class, so the d'tor

  // doesn't need to be virtual.

  ~MyString() { delete[] c_string_; }

  // Gets the 0-terminated C string this MyString object represents.

  const char * c_string() const { return c_string_; }

  size_t Length() const {
    return c_string_ == NULL ? 0 : strlen(c_string_);
  }

  // Sets the 0-terminated C string this MyString object represents.

  void Set(const char * c_string);
};



  // Copy c'tor

  MyString(const MyString& string) : c_string_(NULL) {
    Set(string.c_string_);
  }


这个Set函数中的参数访问了私有成员吧? 能解释下这是啥用法么?
作者: foolishx    时间: 2009-07-28 15:12
这是地道的C++语法,如果类的成员函数都不能访问它的私有成员的话,请问私有成员该怎么访问?
作者: openspace    时间: 2009-07-28 15:15
标题: 回复 #1 aaaaal 的帖子
私有是对类外部来说的
要是不能访问,怎么初始化数据成员
作者: pagx    时间: 2009-07-28 15:28
完全不能访问的变量, 除了用来玩弄技巧之外,想不出能有何用处。
作者: xiaoQ008    时间: 2009-07-28 16:50
提示: 作者被禁止或删除 内容自动屏蔽
作者: fireman119    时间: 2009-07-28 16:58
私有成员不这么访问,那要怎么访问,这里的Set也就是c#之流中属性的取值函数的意思。
作者: struggle    时间: 2009-07-28 17:03
标题: 回复 #1 aaaaal 的帖子
私有不能访问是对其他类而言的,同种类是可以访问的。
作者: aaaaal    时间: 2009-07-28 17:25
MyString(const MyString& string) : c_string_(NULL) {
    Set(string.c_string_);
  }

这里是通过对象访问的吧?
string是某个MyString的对象的引用吧?
作者: fireman119    时间: 2009-07-28 17:44
拷贝构造函数,这里是对象
作者: aaaaa5aa    时间: 2009-07-28 18:47
原帖由 aaaaal 于 2009-7-28 17:25 发表
MyString(const MyString& string) : c_string_(NULL) {
    Set(string.c_string_);
  }

这里是通过对象访问的吧?
string是某个MyString的对象的引用吧?

string是某个MyString的对象
作者: aaaaal    时间: 2009-07-28 22:58
标题: 回复 #10 aaaaa5aa 的帖子
拷贝构造函数为何可以调用引用参数对象的私有成员?
作者: OwnWaterloo    时间: 2009-07-28 23:43
标题: 回复 #11 aaaaal 的帖子
我还是好心把以前的帖子搜出来吧……
http://bbs.chinaunix.net/viewthread.php?tid=1473750#pid10665029



以下是对以前发帖的补充。

C++标准有一整章(11 Member access control)是讲这个的。
下面是一部分:
A member of a class can be
— private; that is, its name can be used only by members and friends of the class in which it is
declared.
— protected; that is, its name can be used only by members and friends of the class in which it is
declared, and by members and friends of classes derived from this class (see 11.5).
— public; that is, its name can be used anywhere without access restriction.


c_string_ 是MyString的一个private成员。它的名字只能被MyString的成员和友元使用。
复制构造函数是MyString的一个“成员(函数)”, 该成员函数能使用c_string_这个名字并没有什么不合理之处。



《The C++ Programming Language》中的描述似乎更好一些:
15.3 Access Control [hier.access]
A member of a class can be p r i v a t e , p r o t e c t e d , or p u b l i c :
– If it is p r i v a t e , its name can be used only by member functions and friends of the class in
which it is declared.
– If it is p r o t e c t e d , its name can be used only by member functions and friends of the class in
which it is declared and by member functions and friends of classes derived from this class
(see §11.5).
– If it is p u b l i c , its name can be used by any function.


更加强调了, 访问级限制的是函数。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2