cpp이랑은 안친한데 크윽 ㅠㅠ
컴파일러나 Cxx 적용 버전의 차이겠지만
$ g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
g++ 4.6.3 에서는 scope 인식을 하긴 하는데
$ cat enum.cpp #include <iostream>
class A { public: int a; enum { A_1, A_2, A_3 }; };
class B { public: int b; enum { B_1, B_2, B_3 };
};
int main() { A a; B b;
a.a = B_1; a.a = A_1;
b.b = A_1; b.b = B_1; return 0; } |
$ g++ enum.cpp enum.cpp: In function ‘int main()’: enum.cpp:33:8: error: ‘B_1’ was not declared in this scope enum.cpp:34:8: error: ‘A_1’ was not declared in this scope |
clang에서는 scope를 조금더 정밀하게 따지는 듯?
$ clang --analyze enum.cpp enum.cpp:33:8: error: use of undeclared identifier 'B_1' a.a = B_1; ^ enum.cpp:34:8: error: use of undeclared identifier 'A_1' a.a = A_1; ^ enum.cpp:36:8: error: use of undeclared identifier 'A_1' b.b = A_1; ^ enum.cpp:37:8: error: use of undeclared identifier 'B_1' b.b = B_1; ^ 4 errors generated. |
scope만 잡아주면.. public에서 선언한거라 문제없이 되는건가?
$ cat enum.cpp #include <iostream>
class A { public: int a; enum { A_1, A_2, A_3 }; };
class B { public: int b; enum { B_1, B_2, B_3 };
};
int main() { A a; B b;
a.a = B::B_1; a.a = A::A_1;
b.b = A::A_1; b.b = B::B_1; return 0; } |
$ g++ enum.cpp $ clang --analyze enum.cpp |
enum 자체에 private를 줘보니..
에러 뿜뿜!
$ cat enum.cpp #include <iostream>
class A { public: int a;
private: enum { A_1, A_2, A_3 }; };
class B { public: int b;
private: enum { B_1, B_2, B_3 };
};
int main() { A a; B b;
a.a = B::B_1; a.a = A::A_1;
b.b = A::A_1; b.b = B::B_1; return 0; } |
$ g++ enum.cpp enum.cpp: In function ‘int main()’: enum.cpp:25:3: error: ‘B::<anonymous enum> B::B_1’ is private enum.cpp:37:11: error: within this context enum.cpp:11:3: error: ‘A::<anonymous enum> A::A_1’ is private enum.cpp:38:11: error: within this context enum.cpp:11:3: error: ‘A::<anonymous enum> A::A_1’ is private enum.cpp:40:11: error: within this context enum.cpp:25:3: error: ‘B::<anonymous enum> B::B_1’ is private enum.cpp:41:11: error: within this context |
$ clang --analyze enum.cpp enum.cpp:37:11: error: 'B_1' is a private member of 'B' a.a = B::B_1; ^ enum.cpp:25:3: note: declared private here B_1, ^ enum.cpp:38:11: error: 'A_1' is a private member of 'A' a.a = A::A_1; ^ enum.cpp:11:3: note: declared private here A_1, ^ enum.cpp:40:11: error: 'A_1' is a private member of 'A' b.b = A::A_1; ^ enum.cpp:11:3: note: declared private here A_1, ^ enum.cpp:41:11: error: 'B_1' is a private member of 'B' b.b = B::B_1; ^ enum.cpp:25:3: note: declared private here B_1, ^ 4 errors generated. |
결론
public일 경우 당연히(!) 다른 클래스에서도 사용 가능,
private일 때는 당연히(!) 다른 클래스에서는 사용 불가