[완료] C++ Primer 4쇄를 보면서, 클래스 타입에 대한 컨스트럭터를 이용한 초기화 란것이 대체 무엇인가요?
Cpp Primer로 공부중에 도무지 이해가 되지 않는 부분이 있어서 질문을 올립니다.
The declaration of myobj compiles without complaint. However, when we try to use myobjSales_item myobj(); // ok: but defines a function, not an object
if (myobj.same_isbn(Primer_3rd_ed)) // error: myobj is a functionthe 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분요리 땡! 하듯이 끝나는것이 아닌가요?
대체 생성도 되지 않은 클래스 타입을 그 클래스 타입으로 생성된 다른 오브젝트에 대입을 한다는것이 제 머리속에서 에러를 내고 있습니다.
class 의 constructor 를
class 의 constructor 를 직접 사용하여 class 를 초기화 하는 예제인듯 합니다만..
전자처럼 초기화 하고자해도 ( 인자가 없는 ctor 의 경우겠죠 ) 마치 함수의 정의처럼 보이기 때문에 compiler error 를 유발 하게 된다는 의미입니다.
따라서 직접 원하는 ctor 를 사용하면서 object를 바로 instance 화 하고 싶다면 후자식으로 하라는 것 입니다.
저 경우 직접 copy constructor 가 불리우는지 log 를 찍어보면서 공부를 하시면 좀 더 명확히 아실 수 있겠지요.
Neogeo - Future is Now.
Neogeo - Future is Now.
알려주셔서
알려주셔서 감사합니다.
카피 컨스트럭터는 14 챕터 정도에 있는거 같네요.
뭐 아직 감은 잘 안오지만 열심히 해보겠습니다.
======= ======= ======= ======= =======
홈즈, 그만 좀 따라다니게나.
from Sherlock Holmes :)
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!
Thanks for your concern..
Thanks for your concern..
======= ======= ======= ======= =======
홈즈, 그만 좀 따라다니게나.
http://kldp.org/node/73060
http://kldp.org/node/73060
일부러
일부러 공부해보시라고 슬쩍 흘려만 드렸는데;;; 더 좋은 참고 자료가 있었군요!
Neogeo - Future is Now.
Neogeo - Future is Now.
댓글 달기