h********l 发帖数: 67 | 1 下面是c++ essential里的例子:
class Triangular_iterator
{
public:
Triangular_iterator( int index ): _index(index-1){}
bool operator==( const Triangular_iterator& ) const;
private:
int _index;
};
inline bool Triangular_iterator::operator==(const Triangular_iterator& rhs )
const
{
return _index==rhs._index;
}
我的问题是: _index是private member, 为什么在operator==里可以直接用rhs._index
呢?
谢谢. |
R********y 发帖数: 288 | 2 因为==是类成员函数啊
)
【在 h********l 的大作中提到】 : 下面是c++ essential里的例子: : class Triangular_iterator : { : public: : Triangular_iterator( int index ): _index(index-1){} : bool operator==( const Triangular_iterator& ) const; : private: : int _index; : }; : inline bool Triangular_iterator::operator==(const Triangular_iterator& rhs )
|
h********l 发帖数: 67 | 3 是不是说,类成员函数可以访问同类的object中私有变量?
const Triangular_iterator& rhs
【在 R********y 的大作中提到】 : 因为==是类成员函数啊 : : )
|
t****t 发帖数: 6806 | 4 member is class member, not object member.
【在 h********l 的大作中提到】 : 是不是说,类成员函数可以访问同类的object中私有变量? : const Triangular_iterator& rhs
|
h********l 发帖数: 67 | 5 刚接触c++,能否多解释一些?
==可以访问_index,我可以理解。但rhs是该类的一个object,
==是怎样访问rhs中的私有成员?
【在 t****t 的大作中提到】 : member is class member, not object member.
|
t****t 发帖数: 6806 | 6 i said it's class member, not object member.
i.e. doesn't matter which object you access as long as they are of this
class.
please read some basic c++ introductory book for more details.
【在 h********l 的大作中提到】 : 刚接触c++,能否多解释一些? : ==可以访问_index,我可以理解。但rhs是该类的一个object, : ==是怎样访问rhs中的私有成员?
|
D*****a 发帖数: 2847 | 7 私有是按类的
不是按对象的
同类的俩对象可以互相访问私有成员
【在 h********l 的大作中提到】 : 刚接触c++,能否多解释一些? : ==可以访问_index,我可以理解。但rhs是该类的一个object, : ==是怎样访问rhs中的私有成员?
|
h********l 发帖数: 67 | 8 Can you recommend some books? I am reading essential c++ now.
Thanks.
【在 t****t 的大作中提到】 : i said it's class member, not object member. : i.e. doesn't matter which object you access as long as they are of this : class. : please read some basic c++ introductory book for more details.
|
t****t 发帖数: 6806 | 9 heard it's ok.
i suggest you read it *carefully*, e.g. don't try to finish a chapter in 1
day.
【在 h********l 的大作中提到】 : Can you recommend some books? I am reading essential c++ now. : Thanks.
|
h********l 发帖数: 67 | 10 Thanks.
【在 D*****a 的大作中提到】 : 私有是按类的 : 不是按对象的 : 同类的俩对象可以互相访问私有成员
|
h********l 发帖数: 67 | 11 Thanks.
【在 t****t 的大作中提到】 : heard it's ok. : i suggest you read it *carefully*, e.g. don't try to finish a chapter in 1 : day.
|