[완료] C++ Primer 4쇄를 보면서, 클래스 타입에 대한 컨스트럭터를 이용한 초기화 란것이 대체 무엇인가요?

MORIARTY의 이미지

Cpp Primer로 공부중에 도무지 이해가 되지 않는 부분이 있어서 질문을 올립니다.

Quote:

The declaration of myobj compiles without complaint. However, when we try to use myobj

Sales_item myobj(); // ok: but defines a function, not an object
if (myobj.same_isbn(Primer_3rd_ed)) // error: myobj is a function

the compiler complains that we cannot apply member access notation to a function! The problem is that our definition of myobj is interpreted by the compiler as a declaration of a function taking no parameters and returning an object of type Sales_itemhardly what we intended! The correct way to define an object using the default constructor is to leave off the trailing, empty parentheses:

// ok: defines a class object ...
Sales_item myobj;

On the other hand, this code is fine:

// ok: create an unnamed, empty Sales_itemand use to initialize myobj
Sales_item myobj = Sales_item();

Here we create and value-initialize a Sales_item object and to use it to initialize myobj. The compiler value-initializes a Sales_item by running its default constructor.

위와 같은 문맥인데요.

도무지 저걸 어디에 쓰이는것이고...

무슨 개념인지를 도무지 개념 차체를 못잡겠습니다.

아니 도대체 왜 쓰이는지도 모르겠어요.

그냥 이렇게 그 클래스에 대한 컨스트럭터 초기화 리스트(Constructor initiallizer list) 기능을 이용하면 3분요리 땡! 하듯이 끝나는것이 아닌가요?

대체 생성도 되지 않은 클래스 타입을 그 클래스 타입으로 생성된 다른 오브젝트에 대입을 한다는것이 제 머리속에서 에러를 내고 있습니다.

neogeo의 이미지

class 의 constructor 를 직접 사용하여 class 를 초기화 하는 예제인듯 합니다만..

전자처럼 초기화 하고자해도 ( 인자가 없는 ctor 의 경우겠죠 ) 마치 함수의 정의처럼 보이기 때문에 compiler error 를 유발 하게 된다는 의미입니다.

따라서 직접 원하는 ctor 를 사용하면서 object를 바로 instance 화 하고 싶다면 후자식으로 하라는 것 입니다.

저 경우 직접 copy constructor 가 불리우는지 log 를 찍어보면서 공부를 하시면 좀 더 명확히 아실 수 있겠지요.

Neogeo - Future is Now.

Neogeo - Future is Now.

MORIARTY의 이미지

알려주셔서 감사합니다.

카피 컨스트럭터는 14 챕터 정도에 있는거 같네요.

뭐 아직 감은 잘 안오지만 열심히 해보겠습니다.

======= ======= ======= ======= =======
홈즈, 그만 좀 따라다니게나.

Ooryll Qrygg의 이미지

The correct way to define an object using the DEFAULT constructor:
if you want to get what you intended, just leave off trailing, empty parentheses!

MORIARTY의 이미지

Thanks for your concern..

======= ======= ======= ======= =======
홈즈, 그만 좀 따라다니게나.

cleol의 이미지

neogeo의 이미지

일부러 공부해보시라고 슬쩍 흘려만 드렸는데;;; 더 좋은 참고 자료가 있었군요!

Neogeo - Future is Now.

Neogeo - Future is Now.

댓글 달기

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