64,651
社区成员
发帖
与我相关
我的任务
分享
#include <iostream.h>
class point
{
public:
point() : x(0),y(0){}
point(point &p)
{
this->x=p.x;
this->y=p.y;
}
void move(int xx,int yy)
{
this->x+=xx;
this->y+=yy;
}
void show()
{
cout<<"("<<this->x<<","<<this->y<<")"<<endl;
}
private:
int x;
int y;
};
//point的封装
class ArrayPoint
{
public:
ArrayPoint(int iArraySize)
{
this->size=iArraySize;
this->pPoint=new point[iArraySize];
}
~ArrayPoint()
{
delete [] pPoint;
}
/**************************************************************************************
问题如下:1,返回point,即point operator [](int index),此时point[0].show()显示结果为(0,0)
2,返回&point,即point &operator [](int index),此时point[0].show()显示结果为(10,10)
预期值为显示结果为(10,10) ,但是为什么第一个返回对象就不行,不能移动指定的点呢?
请见解深刻的大虾帮忙解答返回这两个形式对后续操作的影响和注意,效率方面就不要讲了,
返回引用肯定效率高现在的问题就是第一种为什么得不到预期的结果。
请将下面代码point &operator [](int index)改为point operator [](int index)运行并解答之!
*****************************************************************************************/
//重载[]操作符
point &operator [](int index)
{
return pPoint[index];
}
private:
point *pPoint;
int size; //对象数组的容量
};
int main()
{
ArrayPoint point(10);
point[0].move(10,10);
point[0].show();
return 0;
}
#include "stdafx.h"
#include <iostream.h>
class point
{
public:
point() : x(0),y(0){cout<<"构造函数"<<endl;}
point(point &p)
{
this->x=p.x;
this->y=p.y;
cout<<"拷贝构造函数"<<endl;
}
~point(){cout<<"析构函数"<<endl;}
void move(int xx,int yy)
{
this->x+=xx;
this->y+=yy;
}
void show()
{
cout<<"("<<this->x<<","<<this->y<<")"<<endl;
}
private:
int x;
int y;
};
//point的封装
class ArrayPoint
{
public:
ArrayPoint(int iArraySize)
{
this->size=iArraySize;
this->pPoint=new point[iArraySize];
}
~ArrayPoint()
{
delete [] pPoint;
}
//重载[]操作符
point operator [](int index)
{
return pPoint[index];
}
private:
point *pPoint;
int size; //对象数组的容量
};
int main(int argc, char* argv[])
{
ArrayPoint point(1);
point[0].move(10,10);
point[0].show();
return 0;
}
输出
构造函数
拷贝构造函数
析构函数
拷贝构造函数
(0,0)
析构函数
析构函数
[/quote]哎 失误 失误!调用了 问题解决了返回对象的话只是一个临时的对象而不是类对象本身了,返回引用才是本身
那我就有一个疑问了,既然返回了副本,但是我在point里面添加复制构造函数,测试发现并没有在main()函数中调用到point类的复制构造函数,不是说返回临时对象编译器会点用复制构造函数来保存整个临时对象的吗? 不是,你的[]操作符返回对象的话,返回一个ArrayPoint类成员的一个元素,函数其实是生成一个point的副本,这个副本的值和你返回的那个数组元素一样,也就是说你的[]操作返回的是一个副本而不是类成员本身,所以[].move就操作不到本身了,总是那个副本。如果返回引用那么就是本身了,就正确了 你的意思是move函数没有操作到point(0)? 返回对象的话只是一个临时的对象而不是类对象本身了,返回引用才是本身
#include "stdafx.h"
#include <iostream.h>
class point
{
public:
point() : x(0),y(0){cout<<"构造函数"<<endl;}
point(point &p)
{
this->x=p.x;
this->y=p.y;
cout<<"拷贝构造函数"<<endl;
}
~point(){cout<<"析构函数"<<endl;}
void move(int xx,int yy)
{
this->x+=xx;
this->y+=yy;
}
void show()
{
cout<<"("<<this->x<<","<<this->y<<")"<<endl;
}
private:
int x;
int y;
};
//point的封装
class ArrayPoint
{
public:
ArrayPoint(int iArraySize)
{
this->size=iArraySize;
this->pPoint=new point[iArraySize];
}
~ArrayPoint()
{
delete [] pPoint;
}
//重载[]操作符
point operator [](int index)
{
return pPoint[index];
}
private:
point *pPoint;
int size; //对象数组的容量
};
int main(int argc, char* argv[])
{
ArrayPoint point(1);
point[0].move(10,10);
point[0].show();
return 0;
}
输出
构造函数
拷贝构造函数
析构函数
拷贝构造函数
(0,0)
析构函数
析构函数
不是,你的[]操作符返回对象的话,返回一个ArrayPoint类成员的一个元素,函数其实是生成一个point的副本,这个副本的值和你返回的那个数组元素一样,也就是说你的[]操作返回的是一个副本而不是类成员本身,所以[].move就操作不到本身了,总是那个副本。如果返回引用那么就是本身了,就正确了 你的意思是move函数没有操作到point(0)? 返回对象的话只是一个临时的对象而不是类对象本身了,返回引用才是本身
你的意思是move函数没有操作到point(0)? 返回对象的话只是一个临时的对象而不是类对象本身了,返回引用才是本身
返回对象的话只是一个临时的对象而不是类对象本身了,返回引用才是本身