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값이 들어가는 겁니까???

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.