Redian新闻
>
请教: class private data member
avatar
请教: class private data member# Java - 爪哇娇娃
f*e
1
欢迎加入MaiMaiJiao1俱乐部,一起探讨电商交易物流等问题。
交流信息
avatar
g*d
2
有一个疑问,比如下面的代码:
class A{
public:
A();
A(const A& rhs){ ia = rhs.a; }//rhs is reference
private:
int a;
};
问题是,既然a 是私有的成员,但是为什么在定义copy constructor时,rhs能够直接引
用呢(即rhs.a)?
avatar
C*g
3
问错地方了?java不用const,用final
avatar
g*d
4
不好意思,是C++的问题。希望高人指点一下。

【在 C********g 的大作中提到】
: 问错地方了?java不用const,用final
avatar
O*e
5
道理是一样的。private这样的访问控制符是就类而言的,跟哪个对象无关。

【在 g******d 的大作中提到】
: 不好意思,是C++的问题。希望高人指点一下。
avatar
q*u
6
我来一个具体的好了,看了一下programming和这里的,发现自己懂和表达清楚完全两
码事起。其中有一个老兄凶神恶煞的,看了都一怔。
#include
using namespace std;
class T
{
public:
T(){a = 11;}
int Get() const{return a;}
private:
int a;
};
class Test
{
public:
Test(int a):m_a(a){}
Test(const Test &t){m_a = t.m_a;} //拷贝构造函数,通过同类型的引用参数访问
私有变量
Test& operator=(const Test &t) //赋值操作函数,通过同类型的引用参数访问私
有变量
{
m_a = t.m_a;
}
//Test(const T &t){m_a = t.a;} //此语句出错,不能访问T类型的私有成员
Test(const T &t){m_a = t.Get();} //这个实现可以
void Print() const
{
cout<
【在 O**e 的大作中提到】
: 道理是一样的。private这样的访问控制符是就类而言的,跟哪个对象无关。
avatar
g*d
7
谢谢mahu! 读了好几遍,总算弄懂了!
原来我老是觉得,类外如果要get access to private member,就要有一个接口,比如用T.get_value()什么的,不能直接T.a。
十分感谢!!

【在 q*********u 的大作中提到】
: 我来一个具体的好了,看了一下programming和这里的,发现自己懂和表达清楚完全两
: 码事起。其中有一个老兄凶神恶煞的,看了都一怔。
: #include
: using namespace std;
: class T
: {
: public:
: T(){a = 11;}
: int Get() const{return a;}
: private:

avatar
O*e
8
这个不是“方便”,而是必须。如果访问同类型其它对象的私有成员也只能通过公有权
限的方法,那就意味着你必须写一个公有权限的方法,而本来需要私有化藏起来的成员
就被这个公有权限的办法给暴露了。

【在 q*********u 的大作中提到】
: 我来一个具体的好了,看了一下programming和这里的,发现自己懂和表达清楚完全两
: 码事起。其中有一个老兄凶神恶煞的,看了都一怔。
: #include
: using namespace std;
: class T
: {
: public:
: T(){a = 11;}
: int Get() const{return a;}
: private:

avatar
q*u
9
对的

这个不是“方便”,而是必须。如果访问同类型其它对象的私有成员也只能通过公有权
限的方法,那就意味着你必须写一个公有权限的方法,而本来需要私有化藏起来的成员
就被这个公有权限的办法给暴露了。

【在 O**e 的大作中提到】
: 这个不是“方便”,而是必须。如果访问同类型其它对象的私有成员也只能通过公有权
: 限的方法,那就意味着你必须写一个公有权限的方法,而本来需要私有化藏起来的成员
: 就被这个公有权限的办法给暴露了。

avatar
l*s
10
If it has to be exposed at some time point, maybe it shall not classified as
private.

【在 O**e 的大作中提到】
: 这个不是“方便”,而是必须。如果访问同类型其它对象的私有成员也只能通过公有权
: 限的方法,那就意味着你必须写一个公有权限的方法,而本来需要私有化藏起来的成员
: 就被这个公有权限的办法给暴露了。

avatar
O*e
11
man, did you ever read the original post...

as

【在 l*********s 的大作中提到】
: If it has to be exposed at some time point, maybe it shall not classified as
: private.

avatar
l*s
12
I did; but you sounded like this point is foolproof which I beg to differ.
For analogy, you and I are both human beings, but I don't and
shall not have reading access to your under-ware. Lz's original intuition is
understandable.

【在 O**e 的大作中提到】
: man, did you ever read the original post...
:
: as

avatar
h*0
13
you analogy is not right. the relation between class and instances, are
mostly different from between human and individuals. basically, instances
are all cloned from the same template, and they all have the same behavior.

is

【在 l*********s 的大作中提到】
: I did; but you sounded like this point is foolproof which I beg to differ.
: For analogy, you and I are both human beings, but I don't and
: shall not have reading access to your under-ware. Lz's original intuition is
: understandable.

avatar
s*n
14
while the programmer is the god, he decides what is or is not visible from
one human to another. if he doesn't want "this" reading another's underwear,
he simply doesn't code such reads in the methods. he can prevent other gods
from seeing his people's underwears by declaring "private".

is

【在 l*********s 的大作中提到】
: I did; but you sounded like this point is foolproof which I beg to differ.
: For analogy, you and I are both human beings, but I don't and
: shall not have reading access to your under-ware. Lz's original intuition is
: understandable.

avatar
l*s
15
well,it is OK with me that access control is implemented on the classes,
instead on the instances.
Nonetheless the latter is a desirable goal for the sake of encapsulation.
Say there is a Connection class to represent internet traffic, would you
want my instance of Connection to have full access to the private data in your
instance?

【在 h*****0 的大作中提到】
: you analogy is not right. the relation between class and instances, are
: mostly different from between human and individuals. basically, instances
: are all cloned from the same template, and they all have the same behavior.
:
: is

avatar
h*0
16
sounds like you would let a internet connection to have full access to all
your public method...

your

【在 l*********s 的大作中提到】
: well,it is OK with me that access control is implemented on the classes,
: instead on the instances.
: Nonetheless the latter is a desirable goal for the sake of encapsulation.
: Say there is a Connection class to represent internet traffic, would you
: want my instance of Connection to have full access to the private data in your
: instance?

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