免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: aaaaal
打印 上一主题 下一主题

私有成员可以这样访问? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 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函数中的参数访问了私有成员吧? 能解释下这是啥用法么?

论坛徽章:
0
2 [报告]
发表于 2009-07-28 17:25 |显示全部楼层
MyString(const MyString& string) : c_string_(NULL) {
    Set(string.c_string_);
  }

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

论坛徽章:
0
3 [报告]
发表于 2009-07-28 22:58 |显示全部楼层

回复 #10 aaaaa5aa 的帖子

拷贝构造函数为何可以调用引用参数对象的私有成员?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP