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 的大作中提到】
: 道理是一样的。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 的大作中提到】
: 道理是一样的。private这样的访问控制符是就类而言的,跟哪个对象无关。
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:
原来我老是觉得,类外如果要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:
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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: 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?
相关阅读
question: how to implement thisASP和java也不是互相排斥啊。anyone use org.gxos package?急问:java如何处理中文字符JDK Source?java,生活及其它求Jbuilder9的serial number and KEY!thanks哪位大侠有PM1 Quadtree java source code ?[转载] create gif/png image in C or javaA question for Java certificate!c到java的转换还有java的怪异错误[转载] 如何得到machine specific的信息help on this scope questionJ2EE Deploy问题SunONE Studio5java parallel databaseStress test tools.[Share] Java ScreenShot Application紧急求助: java OutOfMemoryErrorjavac命令行中path的空格问题