C++ copy constructor에 대해서 질문있습니다.

다음과 같은 소스가 있습니다. 계산기 짤려다가 중간에 질문이 생겨서 올립니다.
infinite.h
#ifndef _INFINITE_H_
#define _INFINITE_H
#define MAX 100
using namespace std;
class Infinite
{
public
Infinite(char *strings = "");
~Infinite();
Infinite(const Infinite& infinite);
Infinite operator+(const Infinite& target);
Infinite operator-(const Infinite& target);
Infinite operator-();
Infinite operator*(const Infinite& target);
Infinite operator/(const Infinite& target);
Infinite operator%(const Infinite& target);
Infinite& operator=(const Infinite& target);
friend ostream& operator<<(ostream& out, const Infinite& target);
friend istream& operator>>(istream& in, Infinite& target);
private
char *buffer;
int length;
int totalLength;
int compare(const Infinite& target) const;
static int number;
};
#endif
infinite.cc
#include
#include
using namespace std;
#include "infinite.h"
int Infinitenumber = 0;
InfiniteInfinite(char *strings)
{
length = strlen(strings);
buffer = new char[length + 1];
strcpy(buffer, strings);
number++;
cout << "Constructor number " << number <
}
InfiniteInfinite(const Infinite& target)
{
length = target.length;
buffer = new char[length + 1];
strcpy(buffer, target.buffer);
number++;
cout << "This is a copy constructor " <
}
Infinite~Infinite()
{
delete[] buffer;
number--;
cout << "Destructor number " << number << endl;
}
Infinite Infiniteoperator+(const Infinite& target)
{
totalLength = compare(target);
char *tmpbuffer = new char[totalLength];
for (int i = totalLength ; i > 0 ; i++) {
}
}
Infinite Infiniteoperator-(const Infinite& target)
{
}
Infinite Infiniteoperator-()
{
char *tmpbuffer = new char[length + 2];
strcpy(tmpbuffer, "-");
strcat(tmpbuffer, buffer);
delete[] buffer;
buffer = new char[length + 2];
strcpy(buffer, tmpbuffer);
cout << "This is - operation" <
return Infinite(buffer);
}
Infinite Infiniteoperator*(const Infinite& target)
{
}
Infinite Infiniteoperator/(const Infinite& target)
{
}
Infinite Infiniteoperator%(const Infinite& target)
{
}
Infinite& Infiniteoperator=(const Infinite& target)
{
cout << "This is a = operator " <
if (this == &target) {
return *this;
}
delete[] buffer;
length = target.length;
buffer = new char[length + 1];
strcpy(buffer, target.buffer);
return *this;
}
ostream& operator<<(ostream& out, const Infinite& target)
{
out << target.buffer;
return out;
}
istream& operator>>(istream& in, Infinite& target)
{
char *temp = new char[MAX];
//in >> temp;
in >> temp;
delete[] target.buffer;
target.buffer = new char[strlen(temp) + 1];
strcpy(target.buffer, temp);
return in;
}
int Infinitecompare(const Infinite& target) const
{
return this->length > target.length ? length
(this->length < target.length ? target.length this->length);
}
main.cc
#include
#include "infinite.h"
int main()
{
Infinite figure;
cout << "숫자를 입력 하시오 ";
cin >> figure;
Infinite test(-figure);
cout << figure<
cout << test<
return 0;
}
지금 테스트 할려는 게 -figure와 같은 unary를 테스트해서 나오게 할려고 하는데
infinite test(-figure)라고 하면 operator-에 도달하고 return Infinite(buffer)할 때 객체가
하나 생기고, 그 리턴된 값이 Infinite test(-figure)안에 들어가서 copy constructor를
호출해야 되지 않나요? 그리고 제 생각에는 객체가 3개 생겨야 할 것 같은데, 2
개밖에 생기지 않는군요. Infinite figure할 때 하나 생기고, Infinite test(-figure) 할 때,
operator-에서 Infinite(buffer)를 리턴하면서 하나 생기고, copy constructor할 때 하나
생겨서 총 3개라고 생각됩니다. 그런데 copy constructor는 생기지 않는군요...
제 생각의 어떤 점이 잘 못되었는지 지적해 주시면 감사하겠습니다.
Constructor number 1
숫자를 입력 하시오 2
This is - operation
Constructor number 2
-2
-2
Destructor number 1
Destructor number 0
그리고 Infinite test(figure = -figure)를 할 때는 copy construtor가 호출됩니다. 물론
operator=도 호출되고요... 도대체 전자 처렴 Infinite test(-figure)라고 하면 어떻게
test에 -figure값이 들어가는 겁니까???
댓글 달기