저는 클래스 객체가 정수의 동적 배열 인 곳에서 연산자 함수를 작성하고 있습니다. 연산자는 lhs 및 rhs object를 가져 와서 rhs가 아닌 lhs의 요소 집합 인 객체를 반환합니다. 나는 기능을 작성했지만 소멸자는 객체가 반환 된 직후라고되어 있기 때문에 내가 집합을 반환 할 수없는 생각하지만연산자 함수에서 동적 객체를 반환하는 방법은 무엇입니까?
. 당신이 개체 당신은 값으로 결과를 반환하기 위해 변경해야
IntegerSet::IntegerSet(const int & size)//works correctly
{
capacity = size;
ptr = new int [capacity]();
}
IntegerSet::IntegerSet(const int & size)//works correctly
{
capacity = size;
ptr = new int [capacity]();
}
IntegerSet::IntegerSet(const IntegerSet & copy) : capacity(copy.capacity)//works correctly
{
ptr = copy.clonemaker();
}
IntegerSet::~IntegerSet()
{
capacity = 0;
delete [] ptr;
}
int * IntegerSet::clonemaker() const // works correctly
{
if(ptr==NULL)
{
return NULL;
}
int *tempptr = new int [capacity];
for(int i=0;i<capacity;i++)
{
tempptr[i]=ptr[i];
}
return tempptr;
}
이진 산술 연산자는 값이 아닌 참조를 반환해야합니다. – juanchopanza
참조로 반환하지 마십시오, 즉,'IntegerSet & IntegerSet이 : 운영자'해야'IntegerSet IntegerSet는 :: 연산자는-' – SirGuy
@GuyGreer 덕분 일했다 ... :) 난 그냥 참조 및 const를 반환에 도입되었다 너무 혼란스러운 유형. – Astronautilus