问个关于c++ access 的问题# Programming - 葵花宝典
t*m
1 楼
class Wilma { };
class Fred {
public:
Fred() : p_(new Wilma()) { }
Fred(const Fred& f) : p_(new Wilma(*f.p_)) { }
~Fred() { delete p_; }
Fred& operator= (const Fred& f)
{
// Bad code: Doesn't handle self-assignment!
delete p_; // Line #1
p_ = new Wilma(*f.p_); // Line #2
return *this;
}
private:
Wilma* p_;
};
其中的一句
Fred(const Fred& f) : p_(new Wilma(*f.p_)) { }
为什么可以用*f.p_? p_ 不是f的privat
class Fred {
public:
Fred() : p_(new Wilma()) { }
Fred(const Fred& f) : p_(new Wilma(*f.p_)) { }
~Fred() { delete p_; }
Fred& operator= (const Fred& f)
{
// Bad code: Doesn't handle self-assignment!
delete p_; // Line #1
p_ = new Wilma(*f.p_); // Line #2
return *this;
}
private:
Wilma* p_;
};
其中的一句
Fred(const Fred& f) : p_(new Wilma(*f.p_)) { }
为什么可以用*f.p_? p_ 不是f的privat