C++에서 클래스 변수를 선언할 때 class 키워드를 사용하는 예
변수의 이름이 중복되는 경우 구별하기 위해서 class 키워드를 사용할 수 있다
class T {
public:
class U;
private:
int U;
};
int main()
{
int T;
T t; // error: the local variable T is found
class T t; // OK: finds ::T, the local variable T is ignored
T::U* u; // error: lookup of T::U finds the private data member
class T::U* u; // OK: the data member is ignored
}메소드 파라미터 변수를 선언할 때도 class 키워드를 사용할 수 있다
#include <iostream>
using namespace std;
class ClsA
{
public:
ClsA() {
cout << "Constructor" << endl;
};
ClsA(const ClsA& src) {
cout << "복사 생성자" << endl;
}
~ClsA() {
cout << "Destructor" << endl;
}
void printMsg() {
cout << "C++ Programming" << endl;
}
};
// 아래 함수의 파라미터에 아규먼트 객체가 복사된다(결국 2개의 객체가 생성됨)
// 객체 복사가 필요 없다면 class ClsA& a와 같이 파라미터를 참조형으로 선언하면 된다
void func(class ClsA a) { // class키워드를 사용하거나 안하거나 차이가 없음
a.printMsg();
}
int main()
{
ClsA ca;
func(ca);
return 0;
}