拷贝构造函数。=运算符。

HIGE6 2008-12-03 11:36:57
#include "stdafx.h"
class Point
{
public:
Point(){};
Point(double x, double y):m_x(x),m_y(y){}
Point(const Point& p);

private:
double m_x, m_y;
};

Point::Point(const Point& p)
{
m_x=9;
m_y=p.m_y;
}

void main()
{
Point p2 ;
p2 = Point(3,5);
}

这个时候没有调用拷贝构造函数。
需要重载=运算符?该如果做?
...全文
112 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackzhhuang 2008-12-03
  • 打赏
  • 举报
回复
除非内置类型是指针,这样可以考虑重载=
jackzhhuang 2008-12-03
  • 打赏
  • 举报
回复
内置类型的成员数据最好不要重载=,这样反倒变慢,弄巧成拙。
BaihowFF 2008-12-03
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 luckyvan 的回复:]
类的声明里面似乎可以有引用,不过要在构造函数里面初始化。

引用好像不能用NULL 初始化
[/Quote]
嗯...对 我的意思是...引用必须有实体才可以...null是显然不行的...
luckyvan 2008-12-03
  • 打赏
  • 举报
回复
类的声明里面似乎可以有引用,不过要在构造函数里面初始化。

引用好像不能用NULL 初始化
BaihowFF 2008-12-03
  • 打赏
  • 举报
回复
类里面的声明怎么能用引用呢...
引用必须是有实体的...你可以声明指针...在函数里面在指向实体地址...
HIGE6 2008-12-03
  • 打赏
  • 举报
回复
这样看清楚些

#include "stdafx.h" 

class Pixl
{
public:
Pixl(){};
private:
int m_x;
int m_y;
};

class Point
{
public:
Point(){};
Point(double x, double y):m_x(x),m_y(y){}
Point(const Point& p);

Point& operator=(const Point &src);
private:
double m_x, m_y;
Pixl &pixl;
};

Point& Point::operator=(const Point &src)
{
if(this != &src)
{
m_x=9;
m_y=src.m_y;
}

return *this;
}

Point::Point(const Point& p)
{
m_x=p.m_x;
m_y=p.m_y;
}

void main()
{
Point p2 ;
Point p3 = Point(3,5);
p2 = p3;
}
HIGE6 2008-12-03
  • 打赏
  • 举报
回复
谢谢!
#include "stdafx.h"

class Pixl
{
public:
Pixl(){};
private:
int m_x;
int m_y;
};

class Point
{
public:
Point(){};
Point(double x, double y):m_x(x),m_y(y){}
Point(const Point& p);

Point& operator=(const Point &src);
private:
double m_x, m_y;
Pixl &pixl;
};

Point& Point::operator=(const Point &src)
{
if(this != &src)
{
m_x=9;
m_y=src.m_y;
}

return *this;
}

Point::Point(const Point& p)
{
m_x=p.m_x;
m_y=p.m_y;
}

void main()
{
Point p2 ;
Point p3 = Point(3,5);
p2 = p3;
}

重载=,可以了。不过加上Pixl &pixl;又出现错误,需要在初始化列表里初始化。Point():m_pixl(NULL){};这样写出错。
该如何解决?
BaihowFF 2008-12-03
  • 打赏
  • 举报
回复
如果类里面的变量是简单类型或者成熟的类就不用重载=的
简单类型指int double等内部类型
成熟的指string,vector,map等...或者是自己写的类...但是是很完善的类...可以位拷贝那种....


另外如果你想强行重载=也是可以的
应该这样
Point operator=(const Point &other)
{
m_x=other.m_x;
m_y=other.m_y;
}
xkyx_cn 2008-12-03
  • 打赏
  • 举报
回复

class Point
{
public:
Point(){};
Point(double x, double y):m_x(x),m_y(y){}
Point(const Point& p);
Point& operator=(const Point& other) {
m_x = other.m_x;
m_y = other.m_y;

return *this;
}
private:
double m_x, m_y;
};

Point::Point(const Point& p)
{
m_x=9;
m_y=p.m_y;
}

void main()
{
Point p2 ;
p2 = Point(3,5);
}
luckyvan 2008-12-03
  • 打赏
  • 举报
回复
void main()
{
Point p2 ; //声明的时候已经调用缺省构造函数了
p2 = Point(3,5);
}

所以下面那行是赋值语句;
void main()
{
Point p2 = Point(3,5);
}

这样调用的才是拷贝构造函数,我的理解。

至于重载赋值运算符,取决于你到底要实现什么功能了。

如果只是按位赋值,我觉得用编译器提供的就好了。
  • 打赏
  • 举报
回复
都不需要重载。
xunfeng_2008 2008-12-03
  • 打赏
  • 举报
回复
参考String的写法

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧