멤버들을 모두 기본값으로 초기화 하고 싶을때...

struct SkillData
{
    int id;
    int text_id;
    int casting_type;
    // [...수십개더있음..]
};

SkillData::SkillData()
{
    id = 0;
    text_id = 0;
    casting_type = 0;
    // [...수십개더있음..]
}; 

위의 방법은 멤버 하나 추가할때마다 생성자에도 추가해 줘야하고 깜빡 잊어버리거나 하면 "왜 릴리즈에서만 만 이상하게 돌아가냐구!" 라고 버럭대기 일쑤다. 무엇보다 너무 귀찮아.

물론...

SkillData::SkillData()
{
    memset( this, 0, sizeof(*this) );
}

이런 방법도 있긴 하지만 이건 POD 에만 먹히는 얘기. 나중에 std::string 라거나 혹은 가상함수가 추가되면 난리가 난다. 그래서 생각해 본게..

struct SkillData_Base
{
    int id;
    int text_id;
    int casting_type;
    // [...수십개더있음..]
};

struct SkillData : SkillData_Base
{
    SkillData()
    {
        static SkillData_Base _inst;
        *static_cast< SkillData_Base* >( this ) = _inst;
    }
};

어떨까? -_-;
2006/12/07 20:16 2006/12/07 20:16

트랙백 주소 :: http://testors.net/tt/trackback/762