免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3284 | 回复: 11
打印 上一主题 下一主题

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

论坛徽章:
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 15:12 |只看该作者
这是地道的C++语法,如果类的成员函数都不能访问它的私有成员的话,请问私有成员该怎么访问?

论坛徽章:
1
天蝎座
日期:2013-10-23 21:11:03
3 [报告]
发表于 2009-07-28 15:15 |只看该作者

回复 #1 aaaaal 的帖子

私有是对类外部来说的
要是不能访问,怎么初始化数据成员

论坛徽章:
0
4 [报告]
发表于 2009-07-28 15:28 |只看该作者
完全不能访问的变量, 除了用来玩弄技巧之外,想不出能有何用处。

论坛徽章:
0
5 [报告]
发表于 2009-07-28 16:50 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
6 [报告]
发表于 2009-07-28 16:58 |只看该作者
私有成员不这么访问,那要怎么访问,这里的Set也就是c#之流中属性的取值函数的意思。

论坛徽章:
0
7 [报告]
发表于 2009-07-28 17:03 |只看该作者

回复 #1 aaaaal 的帖子

私有不能访问是对其他类而言的,同种类是可以访问的。

论坛徽章:
0
8 [报告]
发表于 2009-07-28 17:25 |只看该作者
MyString(const MyString& string) : c_string_(NULL) {
    Set(string.c_string_);
  }

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

论坛徽章:
0
9 [报告]
发表于 2009-07-28 17:44 |只看该作者
拷贝构造函数,这里是对象

论坛徽章:
0
10 [报告]
发表于 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的对象
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP