指向包含vector issue的实例类的指针
|
我有这样的课:
class largeInt{
vector<int> myVector;
largeInt operator* (const largeInt &arg);
}
在我的主要工作中,我无法避免使用指针时出现复制:
void main(){
//this works but there are multiple copies: I return a copy of the calculated
//largeInt from the multiplication and then i create a new largeInt from that copy.
largeInt testNum = 10;
largeInt *pNum = new HugeInt( testNum*10);
//i think this code avoid one copy but at the end hI points to a largeInt that has
// myVector = 0 (seems to be a new instance = 0 ). With simple ints this works great.
largeInt i = 10;
largeInt *hI;
hI = &(i*10);
}
我想我正在/不在矢量设计中管理某些东西。
即使不实例化新的largeInt,我也可以实现指针的无复制分配?
谢谢高手!
没有找到相关结果
已邀请:
1 个回复
抬澈帅沮
占用一个临时largeInt的地址,该地址在\'; \'之后立即被破坏-因此
指向无效的内存。 当您将两个
相乘时,您会得到一个新实例-这就是乘法的作用。也许您打算改成ѭ5??那应该修改一个现有实例而不是创建一个新实例。 考虑:
另外,您不应该使用
在堆上创建largeInt \-就像这样:
要么: