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)?
class A{
public:
A();
A(const A& rhs){ ia = rhs.a; }//rhs is reference
private:
int a;
};
问题是,既然a 是私有的成员,但是为什么在定义copy constructor时,rhs能够直接引
用呢(即rhs.a)?
C*g
3 楼
问错地方了?java不用const,用final
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 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 道理是一样的。private这样的访问控制符是就类而言的,跟哪个对象无关。
码事起。其中有一个老兄凶神恶煞的,看了都一怔。
#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 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 道理是一样的。private这样的访问控制符是就类而言的,跟哪个对象无关。
g*d
7 楼
谢谢mahu! 读了好几遍,总算弄懂了!
原来我老是觉得,类外如果要get access to private member,就要有一个接口,比如用T.get_value()什么的,不能直接T.a。
十分感谢!!
【在 q*********u 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 我来一个具体的好了,看了一下programming和这里的,发现自己懂和表达清楚完全两
: 码事起。其中有一个老兄凶神恶煞的,看了都一怔。
: #include
: using namespace std;
: class T
: {
: public:
: T(){a = 11;}
: int Get() const{return a;}
: private:
原来我老是觉得,类外如果要get access to private member,就要有一个接口,比如用T.get_value()什么的,不能直接T.a。
十分感谢!!
【在 q*********u 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 我来一个具体的好了,看了一下programming和这里的,发现自己懂和表达清楚完全两
: 码事起。其中有一个老兄凶神恶煞的,看了都一怔。
: #include
: using namespace std;
: class T
: {
: public:
: T(){a = 11;}
: int Get() const{return a;}
: private:
l*s
12 楼
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 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 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.
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 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 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.
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 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 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.
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 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 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.
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 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 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
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 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 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
h*0
16 楼
sounds like you would let a internet connection to have full access to all
your public method...
your
【在 l*********s 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 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?
your public method...
your
【在 l*********s 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 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?
相关阅读
[合集] 有点糊涂了,一个程序能不能开多个server socket服务?[合集] sleep()和wait()的区别在哪里?关于数组的内存搞java的在加州好找工作么?大概薪水是多少呢? 如果实现文章分页的HTML代码匹配?what's inside an java object?Java runtime 和 java 2 runtime 什么区别?javascript or jstl?JDK US DST Timezone Update Tool有没有在NYC准备考SCJP和SCWCD的?What A DESIGN!网上的J2EE的tutorial大多都太差了这是一个什么用法?jconsole does't list any local java process on Windows xpweb application 如何从数据库取数据?IBM 出的这个Rational Software Architect 和 WebSphere在工作中有用吗?NestedVM 不知阿里巴巴是如何实现静态分页的mitbbs 包子监控机的设计设想及专利声明 (转载).Net developer doing Java