C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 Q&A
C++Builder Programming Q&A
[42660] Re:아니 어째서 이것이 동작을 합니까?
마술감자 [magicpotato] 764 읽음    2005-11-25 09:33
클래스의 static 멤버 변수는 전역변수로 취급해서 처리합니다.
그러니까 a 라는 또다른 네임 스페이스 안에 있는 m_nRef라는 전역변수 정도;

그러니까 쓰신 코드는 a.m_nRef 로 접근하고 있지만 사실 a::m_nRef 로 접근합니다.

레퍼런스가 그렇게 작동한다고 적혀 있습니다.
다음은 빌더5에 있는 Static Member에 대한 설명중 원하시는 내용의 서문입니다.
가장 마지막 문장에 주목하세요.

"static member는 인스턴스가 없어도 네임스페이스로 접근이 가능하다"
"그 외에도 y.x 와 yptr->x 로도 접근이 가능하지만, y와 yptr은 처리되지 않는다." 라고 되어 있습니다.

이유는 적혀있지 않지만, 왜 그렇게 해놨는지 추측할 수 있는 내용입니다.(다만 의견이 분분할듯;;)
참고로 MS Visual C++에서도 빌더와 동일하게 작동합니다.

Static members
The storage class specifier static can be used in class declarations of data and function members. Such members are called static members and have distinct properties from nonstatic members. With nonstatic members, a distinct copy 밻xists?for each instance of the class; with static members, only one copy exists, and it can be accessed without reference to any particular object in its class. If x is a static member of class X, it can be referenced as X::x (even if objects of class X haven뭪 been created yet). It is still possible to access x using the normal member access operators. For example, y.x and yptr->x, where y
is an object of class X and yptr is a pointer to an object of class X, although the expressions y and yptr are not evaluated. In particular, a static member function can be called with or without the special member function syntax:

For example, y.x and yptr->x, where y
is an object of class X and yptr is a pointer to an object of class X, although the expressions y and yptr are not evaluated. In particular, a static member function can be called with or without the special member function syntax:




김상면 님이 쓰신 글 :
: class a
: {public:
:      static int m_nRef;
:      a()
:      {}
:      a(const a& arg)
:      {    arg.m_nRef++
:       }
: };
:
: int a::m_nRef = 0;
:
: void main()
: {      a  aa;
:         a  ab = aa;
: }
:
: 물론 m_nRef가 공유 변수라는 것은 알지만 상수 객체를 통해서 변경된다는게 좀 그렇군요...
: 혹시 다른 이유라도 있나요?
: 그럼

+ -

관련 글 리스트
42657 아니 어째서 이것이 동작을 합니까? 김상면 744 2005/11/24
42660     Re:아니 어째서 이것이 동작을 합니까? 마술감자 764 2005/11/25
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.