프로그래밍 언어/C++

C++ 클래스 접근제한자 관련 보충 내용

ShovelingLife 2022. 11. 7. 19:07

클래스 접근제한자는 오브젝트 레벨이 아닌 클래스 레벨로 작동되기 때문에 생성자 내에서 파라미터로 받는 같은 클래스 변수의 private 또는 protected 멤버 변수에 접근이 가능하다.

#include <iostream>

using namespace std;

class A
{
private:
    int mValue = 0;

public:
    A() = default;

    A(int val) : mValue(val)
    {

    }

    A(const A& a)
    {
        this->mValue = a.mValue;
    }

    void Print()
    {
        cout << mValue << endl;
    }
};

int main()
{
    A a(6), b(a);
    a.Print();
    b.Print();
}