未调用复制构造函数
#include <iostream>
int main(void)
{
class date {
private:
int day;
int month;
int year;
public:
date( ) { std::cout << "default constructor called" << std::endl; }
date& operator=(const date& a) { std::cout << "copy constructor called" << std::endl; day=a.day; month=a.month; year=a.year; }
date(int d ,int m ,int y ) : day(d),month(m),year(y){ std::cout << "constructor called" << std::endl; }
void p_date(){ std::cout << "day=" << day << ",month=" << month << ",year=" << year << std::endl; }
date& add_day(int d) { day += d; return *this;}
date& add_month(int d) { month += d;return *this; }
date& add_year(int d) { year += d;return *this; }
};
class cdate {
date n;
public:
cdate(date b) : n(b) { std::cout << "cdate constructor called" << std::endl;}
void p_cdate() { n.p_date(); }
};
cdate ncdate(date(30,1,2012));
ncdate.p_cdate();
}
当我们在此代码中实例化 ncdate 时:
- 调用
cdate ncdate(date(30,1,2012));
时创建的临时日期对象, - 然后我期望调用
n = b
并期望调用n
的复制构造函数。
n
的复制构造函数没有被调用,我不明白为什么。我知道第二个假设有问题。 注意:这只是测试代码,因此不要讨论其性能、可用性等。
#include <iostream>
int main(void)
{
class date {
private:
int day;
int month;
int year;
public:
date( ) { std::cout << "default constructor called" << std::endl; }
date& operator=(const date& a) { std::cout << "copy constructor called" << std::endl; day=a.day; month=a.month; year=a.year; }
date(int d ,int m ,int y ) : day(d),month(m),year(y){ std::cout << "constructor called" << std::endl; }
void p_date(){ std::cout << "day=" << day << ",month=" << month << ",year=" << year << std::endl; }
date& add_day(int d) { day += d; return *this;}
date& add_month(int d) { month += d;return *this; }
date& add_year(int d) { year += d;return *this; }
};
class cdate {
date n;
public:
cdate(date b) : n(b) { std::cout << "cdate constructor called" << std::endl;}
void p_cdate() { n.p_date(); }
};
cdate ncdate(date(30,1,2012));
ncdate.p_cdate();
}
When we instantiate ncdate
in this code:
- temporary date object created when we call
cdate ncdate(date(30,1,2012));
- then i expect the call
n = b
and expectn
's copy constructor to be called.
n
's copy constructor is not getting called and i cant figure out why. I know there is something wrong in the 2nd assumption. Note: this is test code only so don't go over its performance, usability etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尚未为
date
定义复制构造函数,因此使用隐式声明的复制构造函数。复制构造函数看起来像
date(date const& other) { }
。您已提供默认构造函数 (date()
) 和复制赋值运算符 (date& operator=(const date& a)
)。这些都不是复制构造函数。You have not defined a copy constructor for
date
, so the implicitly-declared copy constructor is used.A copy constructor would look like
date(date const& other) { }
. You have provided a default constructor (date()
) and a copy assignment operator (date& operator=(const date& a)
). Neither of these is the copy constructor.实际上,我在您的代码中没有找到复制构造函数。复制构造函数应声明为 date(date&d),您只需声明一个赋值操作。
actually, I do not found the copy constructor in your code. the copy constructor shall declare as date(date& d), you only declare a assignment operation.
这不是复制构造函数,而是operator=。
复制构造函数如下所示:
This is not a copy constructor, but operator=.
A copy constructor would look like this: