c++ 맴버변수 초기화 관련 질문
조회수 805회
#include "stdafx.h"
#include <iostream>
using namespace std;
class foam
{
public:
int temp_c ;
foam()
{
temp_a = 10;
temp_b = 20;
}
int temp_a ;
int temp_b ;
void printdata()
{
cout << "temp_a : " << temp_a << endl;
cout << "temp_b : " << temp_b << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
foam exam = { 30 };
exam.printdata();
return 0;
}
제가 의도 했던 바는 temp_a 와 temp_b는 foam 클래스에서 생성자에 의해 자동초기화 되고 temp_c는 tmain 함수에서 초기화하는 것을 의도했습니다.
foam exam = { 30 };
에서
"error C2552: 'exam' : 이니셜라이저 목록을 사용하여 비집합체를 초기화할 수 없습니다. "
라는 오류가 발생합니다.
저의 의도처럼 c++ 11 문법에서 클래스 맴버변수중 어떤 변수는 내부에서 초기화되고, 어떤 변수는 _main함수에서 초기화되는 것을 구현할 방법이 있을까요?
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
1 답변
-
Blockquote
아래와 같이 해보세요
생성자
foam(int val) { temp_a = 10; temp_b = 20; temp_c = val; }
클래스 선언
foam exam(30);
댓글 입력