편집 기록

편집 기록
  • 프로필 nowp님의 편집
    날짜2020.03.01

    c++ 맴버변수 초기화 관련 질문


    #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함수에서 초기화되는 것을 구현할 방법이 있을까요?

  • 프로필 알 수 없는 사용자님의 편집
    날짜2020.02.26

    c++ 맴버변수 초기화 관련 질문


    include "stdafx.h"

    include

    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함수에서 초기화되는 것을 구현할 방법이 있을까요?