Redian新闻
>
c++问题,请高人指点迷津,c++ faq lite的一个例子
avatar
c++问题,请高人指点迷津,c++ faq lite的一个例子# Programming - 葵花宝典
p*v
1
class Wilma { };
class Fred {
public:
Fred() : p_(new Wilma()) { }
Fred(const Fred& f) : p_(new Wilma(*f.p_)) { } //question
~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_;
};
上面例子中,不明白在copy constructor (//question 那行)
为什么要用Wilma(*f.p_),这个*f的用法是什么
avatar
c*g
2

Wilma's copy ctor with parameter of const Wilma&
Accessibility control with class, not object
Explained in the previous answer
initialization

【在 p**v 的大作中提到】
: class Wilma { };
: class Fred {
: public:
: Fred() : p_(new Wilma()) { }
: Fred(const Fred& f) : p_(new Wilma(*f.p_)) { } //question
: ~Fred() { delete p_; }
: Fred& operator= (const Fred& f)
: {
: // Bad code: Doesn't handle self-assignment!
: delete p_; // Line #1

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。