C++ 에서 디폴트 복사 생성자와 디폴트 operator = 의 작동 원리가 궁금하네요.

ssehoony의 이미지

클래스에 안에 벡터가 하나 있습니다.
가령 다음과 같은 클래스가 있을 때

class ctest
{
public:
	ctest();
 
	vector<int> list;
};

다음과 같이 사용하게 되면

ctest t1;
t1.list.push_back(1);
t1.list.push_back(2);
t1.list.push_back(3);
 
ctest t2 = t1;

ctest의 디폴트 복사 생성자가 호출이 되어 복사가 되어 질 텐데요.
이런 경우에, list 멤버 변수는 deep copy가 되는건가요? shallow copy가 되는 건가요?
(gcc의 경우는 t2.list = t1.list; 라고 한 것과 같이 vector의 operator = 을 사용하는 듯 작동하던데, Sun CC는 그렇게 작동을 안하는 듯 합니다.)
C++ 표준에 어떻게 작동해야 하는지 명시가 되어 있나요?
꼭 저런것(멤버변수가 deep copy가 되도록 구현 되어 있을때)도 직접 복사 생성자나 할당 연산자를 명시적으로 구현해야 하나요?
thyoo의 이미지

C++98
12.8 Copying class objects
8
class X의 암묵적으로 정의된 copy constructor는
그 subobject를 멤버대 멤버 copy한다.
복사 순서는 사용자 정의 constructor에서의 베이스와 멤버의 초기화
순서와 같다.
각각의 subobject는 다음과 같이 그 타입에 따라 적절하게 copy된다.
- class type 경우, class의 copy constructor를 호출한다.
- array 경우, 각각의 요소가 요소 타입에 적절한 방법으로 복사된다.
- scalar type 경우, built-in assignment 연산자를 사용한다.
Virtual base class의 subobject는 단 한 번만 복사된다.

-->
std::vector
std::list
의 copy ctor이 호출됩니다.

그러므로 copy ctor은 따로 정의할 필요가 없지만
실수를 막기 위해서라도
복사해서는 안되는 class경우, copy ctor과 assignment op를 private으로
복사할 필요가 있는 class경우, copy ctor과 assignment op를 명시적으로 정의하는 게 바람직합니다.
___________________________________
Less is More (Robert Browning)

___________________________________
Less is More (Robert Browning)

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.