由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - [合集] C++ private member question
相关主题
C++里能否在运行时check一个object里的member的名字,并且找出其指针?
相关话题的讨论汇总
话题: class话题: student话题: c++话题: member话题: private
进入Programming版参与讨论
1 (共1页)
c***d
发帖数: 996
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
c*********n
发帖数: 128
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){
: ...

t****t
发帖数: 6806
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难道不是一件很危险的事情么?

c*********n
发帖数: 128
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.

t****t
发帖数: 6806
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

c*********n
发帖数: 128
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

t****t
发帖数: 6806
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

c*********n
发帖数: 128
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

1 (共1页)
进入Programming版参与讨论
相关主题
C++里能否在运行时check一个object里的member的名字,并且找出其指针?
相关话题的讨论汇总
话题: class话题: student话题: c++话题: member话题: private