Redian新闻
>
[合集] C++ private member question
avatar
[合集] C++ private member question# Programming - 葵花宝典
c*d
1
☆─────────────────────────────────────☆
arjiang (得失随缘,心无增减) 于 (Sun Jan 14 04:16:46 2007) 提到:
private member of a class
就是其他object不能直接access,
但如果是同一种class的不同instance呢?
如 Student A, B;
A 应该不能直接读取B的private: score 吧
可是为什么assignment operater 总是这样写
Student& operator=(const Student& rhs){
...
this->score=rhs.score;
...
}
难道assignment operator是个特例,还是我理解有问题?
谢谢解惑
☆─────────────────────────────────────☆
goodbug (好虫) 于 (Sun Jan 14 11:36:46 2007) 提到:
You can access private member in your class imp
avatar
c*n
2
原来刚刚讨论过了。。。。。。我土了。。。。。。
汗。。。。。。
不过我还是不太明白为什么C++以及其他的OOP语言的设计者们把access control设计成
class based 而不是 object based。
intuitively,正如cynod说的:
“如 Student A, B;
A 应该不能直接读取B的private: score 吧”
Student studentA studentB;
在studentB里就可以修改studentA的private data难道不是一件很危险的事情么?

【在 c***d 的大作中提到】
: ☆─────────────────────────────────────☆
: arjiang (得失随缘,心无增减) 于 (Sun Jan 14 04:16:46 2007) 提到:
: private member of a class
: 就是其他object不能直接access,
: 但如果是同一种class的不同instance呢?
: 如 Student A, B;
: A 应该不能直接读取B的private: score 吧
: 可是为什么assignment operater 总是这样写
: Student& operator=(const Student& rhs){
: ...

avatar
t*t
3
怎么还没弄清楚啊.
为什么你要把member function想成是某个object的function呢?
member function is for a class, not for an object. there is nothing like "A
can't access B's private member". A can not access anything, it's the method
of class student that access something. Method of class student happened to
be passed the pointer of A as "this", or the pointer of B as "this". For
example, in Student::print(), score happened to mean this->score. You don't
have to use "this" though, you can use a->score as well.

【在 c*********n 的大作中提到】
: 原来刚刚讨论过了。。。。。。我土了。。。。。。
: 汗。。。。。。
: 不过我还是不太明白为什么C++以及其他的OOP语言的设计者们把access control设计成
: class based 而不是 object based。
: intuitively,正如cynod说的:
: “如 Student A, B;
: A 应该不能直接读取B的private: score 吧”
: Student studentA studentB;
: 在studentB里就可以修改studentA的private data难道不是一件很危险的事情么?

avatar
c*n
4
If that is the issue: member function belongs to *class* instead of *
instance of class*, then let's see what's the situation in Java since I am a
little bit more familiar with Java than with C++ (in which I am quite a
rookie).
In Java, people do differentiate class methods (functions) and instance
methods, or more generally, class members and instance members.
http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html
In my test code in Java
http://www.mitbbs.com/article/Programming/30

【在 t****t 的大作中提到】
: 怎么还没弄清楚啊.
: 为什么你要把member function想成是某个object的function呢?
: member function is for a class, not for an object. there is nothing like "A
: can't access B's private member". A can not access anything, it's the method
: of class student that access something. Method of class student happened to
: be passed the pointer of A as "this", or the pointer of B as "this". For
: example, in Student::print(), score happened to mean this->score. You don't
: have to use "this" though, you can use a->score as well.

avatar
t*t
5
static methods (or you call it class method) just happen to have no "this"
pointer (in C++). but having a "this" sent in does NOT limit your access to
other objects of the same class. They do not "belong to" some instance, they
are still the method of class.

a
members

【在 c*********n 的大作中提到】
: If that is the issue: member function belongs to *class* instead of *
: instance of class*, then let's see what's the situation in Java since I am a
: little bit more familiar with Java than with C++ (in which I am quite a
: rookie).
: In Java, people do differentiate class methods (functions) and instance
: methods, or more generally, class members and instance members.
: http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html
: In my test code in Java
: http://www.mitbbs.com/article/Programming/30

avatar
c*n
6
Well, you are explaining the "why" in a sense of language mechanism but what
I was asking is why the language designer did it so. And this answer by
some other person make some sense:
Mostly because of the different purpose of those. Class-based has the
purpose to prevent coding mistakes, whereas instance-based looks like
a security measure. C++ wasn't designed with security in mind.

to
they
Well, in the sense of their location in the memory, Yes you are right. And
different instances of one

【在 t****t 的大作中提到】
: static methods (or you call it class method) just happen to have no "this"
: pointer (in C++). but having a "this" sent in does NOT limit your access to
: other objects of the same class. They do not "belong to" some instance, they
: are still the method of class.
:
: a
: members

avatar
t*t
7
security... i don't see why it's security. you can get whatever member if
you have pointer to object.
talking about security, java was designed with security in mind, why java
doesn't provide "instance based access control"?

what

【在 c*********n 的大作中提到】
: Well, you are explaining the "why" in a sense of language mechanism but what
: I was asking is why the language designer did it so. And this answer by
: some other person make some sense:
: Mostly because of the different purpose of those. Class-based has the
: purpose to prevent coding mistakes, whereas instance-based looks like
: a security measure. C++ wasn't designed with security in mind.
:
: to
: they
: Well, in the sense of their location in the memory, Yes you are right. And

avatar
c*n
8
en, that's also what I asked......

【在 t****t 的大作中提到】
: security... i don't see why it's security. you can get whatever member if
: you have pointer to object.
: talking about security, java was designed with security in mind, why java
: doesn't provide "instance based access control"?
:
: what

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。