C++ 생성자 멤버 초기화 리스트 (Constructor member initializer list)

 직접 초기화와 유니폼 초기화를 시험해 보았다

 배운 것들을 토대로 이런 저런 것들을 합쳐보았다

#include <iostream>

class Something
{
private:
    int m_value1;
    double m_value2;
    char m_value3;

public:
    Something(int value1, double value2, char value3 = 'c')
    : m_value1(value1),m_value2(value2),m_value3(value3) //직접 초기화 방법
    {
        //여기에 멤버 변수 할당이 불필요함
    }

    ~Something()
    {
        std::cout<<"Something deleted from memories"<<std::endl;
    }

    void print()
    {
        std::cout<<"Something("<<m_value1<<","<<m_value2<<","<<m_value3<<")\n";
    }
};


class Something2
{
private:
    const int m_value;

public:
    Something2():m_value{20} //Uniform initializer
    {
    }
};

class Something3
{
private:
    const int m_array[5];
public:
    Something3():m_array{}
    {
    }
};


class A
{
public:
    A(int x){std::cout<<"A : "<<x<<"\n";}
    ~A()
    {
        std::cout<<"A deleted from memories"<<std::endl;
    }
};

class B
{
private:
    A m_a;
public:
    B(int y):m_a(y-1)
    {
        std::cout<<"B : "<<y<<"\n";
    }
    ~B()
    {
        std::cout<<"B deleted from memories"<<std::endl;
    }
};


int main()
{
    int a;
    std::cout<<"value : ";
    std::cin>> a;
    std::cout<<std::endl;
    B* b = new B(a);

    Something* something = new Something(10, 29.04);
    something->print();

    delete b;
    delete something;


    return 0;
}

댓글

이 블로그의 인기 게시물

About AActor!!! "UObject" has no member "BeginPlay"

UNREAL Android build information

C++ 생성자 위임 (delegating constructor)