본문 바로가기

프로그래밍/C-CPP

[번역|스택오버플로우] 타입명 뒤에 괄호를 붙이는지 안 붙이는지에 차이가 있나?

반응형

https://stackoverflow.com/questions/620137/do-the-parentheses-after-the-type-name-make-a-difference-with-new


[질문] by David Read

Test 가 보통 클래스라면, 아래 두가지에 차이가 있나?


Test* test = new Test;


Test* test = new Test();



[3번 답변] by bayda

일반적으로 첫번째 것은 디폴트-초기화, 두번째 것은 값-초기화가 된다.


예를 들어 int (POD 타입)의 경우

int* test = new int - *test의 값은 무엇이라도 될 수 있다.

int* test = new int() - *test 의 값은 0이 될 것이다.


이외의 동작은 Test 타입이 어떤 것인가에 달렸다. (이하생략)


[원문]

If 'Test' is an ordinary class, is there any difference between:

Test* test = new Test;

and

Test* test = new Test();

In general we have default-initialization in first case and value-initialization in second case.

For example: in case with int (POD type):

  • int* test = new int - we have any initialization and value of *test can be any.

  • int* test = new int() - *test will have 0 value.

next behaviour depended from your type Test. We have defferent cases: Test have defult constructor, Test have generated default constructor, Test contain POD member, non POD member...

728x90