您的位置:首页 > 其它

类成员函数可以访问相同类对象的私有对象

2013-03-19 17:32 393 查看
今天在学习c++ copying函数的时候,了解到这个问题:类成员函数可以访问相同类对象的私有对象;

下面这个例子是很好的copying函数,有很多细节。

实例如下:

class Customer

{

public:

Customer(const Customer& c):

_name(c._name)

{

}

Customer& operator=(const Customer& c)

{

_name = c._name ;

return *this ;

}

private:

std::string _name ;

};

class PriorityCustomer : public Customer

{

public:

PriorityCustomer(const PriorityCustomer& pc):

Customer(pc),

_priority(pc._priority)

{

}

PriorityCustomer& operator=(const PriorityCustomer& pc)

{

Customer::operator=(pc);

_priority = pc._priority;

return *this ;

}

private:

int _priority ;

};

1. 子类实现copying函数一定要调用父类的copying函数,否则会调用一个父类的默认copying函数,导致问题;

2. 可以用子类对象构造父类;

3. 这个例子说明了另外一个问题,类成员函数可以访问相同类对象的私有成员;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐