클래스 멤버 개요

A class 또는 struct 해당 멤버로 구성됩니다. 클래스가 수행하는 작업은 해당 멤버 함수에 의해 수행됩니다. 클래스가 유지하는 상태는 해당 데이터 멤버에 저장됩니다. 멤버 초기화는 생성자에 의해 수행되며, 클린 메모리 해제 및 리소스 해제와 같은 업 작업은 소멸자에 의해 수행됩니다. C++11 이상에서는 선언 지점에 데이터 멤버를 초기화할 수 있으며 일반적으로 초기화해야 합니다.

클래스 멤버 종류

멤버 범주 전체 목록은 다음과 같습니다.

예제 클래스 선언

다음 예제에서는 간단한 구조체 선언을 보여 줍니다.

// TestRun.h

class TestRun
{
    // Start member list.

    // The class interface accessible to all callers.
public:
    // Use compiler-generated default constructor:
    TestRun() = default;
    // Don't generate a copy constructor:
    TestRun(const TestRun&) = delete;
    TestRun(std::string name);
    void DoSomething();
    int Calculate(int a, double d);
    virtual ~TestRun();
    enum class State { Active, Suspended };

    // Accessible to this class and derived classes only.
protected:
    virtual void Initialize();
    virtual void Suspend();
    State GetState();

    // Accessible to this class only.
private:
    // Default brace-initialization of instance members:
    State _state{ State::Suspended };
    std::string _testName{ "" };
    int _index{ 0 };

    // Non-const static member:
    static int _instances;
    // End member list.
};

// Define and initialize static member.
int TestRun::_instances{ 0 };

멤버 접근성

클래스의 멤버는 멤버 목록에 선언됩니다. 클래스의 멤버 목록은 액세스 지정자라고 하는 키워드(keyword) 사용하여 여러 privateprotected 개 및 public 섹션으로 나눌 수 있습니다. 콜론은 : 액세스 지정자를 따라야 합니다. 이러한 섹션은 연속될 필요가 없습니다. 즉, 이러한 키워드(keyword) 구성원 목록에 여러 번 나타날 수 있습니다. 다음 액세스 지정자나 닫는 중괄호가 나올 때까지 키워드가 모든 멤버의 액세스를 지정합니다. 자세한 내용은 멤버 액세스 제어(C++)를 참조하세요.

정적 멤버

데이터 멤버는 정적 멤버로 선언할 수 있습니다. 이 경우 클래스의 모든 개체가 동일한 복사본에 액세스할 수 있게 됩니다. 멤버 함수는 정적 함수로 선언될 수 있으며, 이 경우 클래스의 정적 데이터 멤버에만 액세스할 수 있으며 포인터가 없습니다 this . 자세한 내용은 정적 데이터 멤버를 참조 하세요.

특수 멤버 함수

특수 멤버 함수는 소스 코드에서 지정하지 않으면 컴파일러에서 자동으로 제공하는 함수입니다.

  • 기본 생성자

  • 복사 생성자

  • (C++11) 이동 생성자

  • 복사 할당 연산자

  • (C++11) 이동 대입 연산자

  • Destructor

자세한 내용은 특수 멤버 함수를 참조 하세요.

멤버별 초기화

C++11 이상에서는 비정적 멤버 선언자에 이니셜라이저를 포함할 수 있습니다.

class CanInit
{
public:
    long num {7};       // OK in C++11
    int k = 9;          // OK in C++11
    static int i = 9; // Error: must be defined and initialized
                      // outside of class declaration.

    // initializes num to 7 and k to 9
    CanInit(){}

    // overwrites original initialized value of num:
    CanInit(int val) : num(val) {}
};
int main()
{
}

멤버에 생성자에 값이 할당된 경우 해당 값은 선언 시 할당된 값을 덮어씁니다.

지정된 클래스 형식의 모든 개체에 대해 정적 데이터 멤버의 공유 복사본은 하나뿐입니다. 정적 데이터 멤버를 정의해야 하며 파일 범위에서 초기화할 수 있습니다. 정적 데이터 멤버에 대한 자세한 내용은 정적 데이터 멤버를 참조 하세요. 다음 예제에서는 정적 데이터 멤버를 초기화하는 방법을 보여줍니다.

// class_members2.cpp
class CanInit2
{
public:
    CanInit2() {} // Initializes num to 7 when new objects of type
                 //  CanInit are created.
    long     num {7};
    static int i;
    static int j;
};

// At file scope:

// i is defined at file scope and initialized to 15.
// The initializer is evaluated in the scope of CanInit.
int CanInit2::i = 15;

// The right side of the initializer is in the scope
// of the object being initialized
int CanInit2::j = i;

참고 항목

정의할 CanInit2i 클래스의 멤버가 되도록 지정하려면 클래스 이름 iCanInit2 앞에 와야 합니다.

참고 항목

클래스 및 구조체