클래스와 객체: 초기화 리스트, 타입 변환, 정적 멤버 및 기타

1. 초기화 리스트

C++에서 클래스의 멤버 변수를 초기화할 때는 함수 본문 내에서 할당하는 대신 초기화 리스트를 사용할 수 있습니다. 초기화 리스트는 콜론(:)으로 시작하며, 각 멤버 변수는 괄호 안에 초기값이나 표현식을 포함합니다.

  • 각 멤버 변수는 초기화 리스트에서 한 번만 나타나야 합니다.
  • 참조 멤버 변수, const 멤버 변수, 기본 생성자가 없는 클래스 타입 변수는 반드시 초기화 리스트에서 초기화해야 합니다.
  • C++11에서는 멤버 변수 선언 시 기본값을 지정할 수 있으며, 이 값은 초기화 리스트에서 명시적으로 초기화되지 않은 경우 사용됩니다.
#include <iostream>
using namespace std;

class Time {
public:
    Time(int hour): _hour(hour) {
        cout << "Time()" << endl;
    }
private:
    int _hour;
};

class Date {
public:
    Date(int& x, int year = 1, int month = 1, int day = 1): 
        _year(year), 
        _month(month), 
        _day(day), 
        _t(12), 
        _ref(x), 
        _n(1) {
    }
    void Print() const {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
private:
    int _year = 1;
    int _month = 1;
    int _day;
    Time _t = 1;
    const int _n = 1;
    int* _ptr = (int*)malloc(12);
};

int main() {
    int x = 0;
    Date d1(x);
    d1.Print();
    return 0;
}

2. 타입 변환

C++에서 내장 타입을 클래스 타입으로 암시적으로 변환하려면 해당 타입을 매개변수로 받는 생성자가 필요합니다. 또한, 생성자 앞에 explicit 키워드를 붙이면 암시적 타입 변환을 막을 수 있습니다.

#include <iostream>

int main() {
    int a = 10;
    double b = a; // 암시적 타입 변환
    cout << "Double value: " << b << endl;

    double c = 3.14;
    int d = static_cast<int>(c); // 명시적 타입 변환
    cout << "Integer value: " << d << endl;

    return 0;
}

3. 정적 멤버

정적 멤버 변수는 모든 객체가 공유하며, 클래스 외부에서 초기화해야 합니다. 정적 멤버 함수는 this 포인터가 없으며, 다른 정적 멤버만 접근할 수 있습니다.

#include <iostream>

class MyClass {
public:
    static int count;
    MyClass() { ++count; }
    static void printCount() {
        cout << "Count: " << count << endl;
    }
};

int MyClass::count = 0;

int main() {
    MyClass obj1, obj2;
    MyClass::printCount();
    return 0;
}

4. 친구 클래스 및 함수

친구 함수 또는 친구 클래스는 클래스의 private 및 protected 멤버에 접근할 수 있습니다. 친구 관계는 단방향이며, 전달되지 않습니다.

#include <iostream>

class B;
class A {
    friend void func(const A& aa, const B& bb);
private:
    int _a1 = 1;
    int _a2 = 2;
};

class B {
    friend void func(const A& aa, const B& bb);
private:
    int _b1 = 3;
    int _b2 = 4;
};

void func(const A& aa, const B& bb) {
    cout << aa._a1 << endl;
    cout << bb._b1 << endl;
}

int main() {
    A aa;
    B bb;
    func(aa, bb);
    return 0;
}

5. 내부 클래스

내부 클래스는 다른 클래스 내부에 정의된 클래스입니다. 내부 클래스는 외부 클래스의 범위와 접근 제한자에 의해 제한받습니다.

#include <iostream>

class Outer {
private:
    static int _k;
    int _h = 1;
public:
    class Inner {
    public:
        void foo(const Outer& o) {
            cout << _k << endl;
            cout << o._h << endl;
        }
        int _b1;
    };
};

int Outer::_k = 1;

int main() {
    Outer::Inner in;
    Outer out;
    in.foo(out);
    return 0;
}

6. 익명 객체

익명 객체는 임시 객체로, 그 생명 주기는 해당 줄에서 끝납니다.

#include <iostream>

class Point {
public:
    int x;
    int y;
    Point(int x, int y): x(x), y(y) {}
    void display() {
        cout << "Point(" << x << ", " << y << ")" << endl;
    }
};

int main() {
    Point(3, 4).display(); // 익명 객체
    Point p1(1, 2);
    p1 = Point(5, 6); // 임시 객체로 대입
    return 0;
}

태그: C++ 클래스 객체 초기화 리스트 타입 변환

6월 24일 21:57에 게시됨