2023년 2월 13일 월요일

c++로 짜여진 코드에서 흔히 보는 세 연산자 "::" , "->", "." 의 차이

 c++로 짜여진 코드에서 흔히 보는 세 연산자 "::" , "->", "."   의 차이 


세 연산자 모두 멤버 함수나 변수에 접근하기 위해 사용된다.

그 차이는 접근하는 방법에 있다. 


https://stackoverflow.com/questions/11902791/what-is-the-difference-between-and-in-c


(1) Dot operator is used in direct member selection scenarios.

print(a.b)

Here, we are accessing b, which is a direct member of an object a. So, primarily, a is an object and b is a member (function/ variable etc) of a.

즉 여기서 a 는 object 


(2) Arrow operator is used in indirect member selection scenarios.

print(a->b)

Here, we are accessing b which is a member of the object, that is pointed to by a. It is shorthand of (*a).b and so here, a is primarily a pointer to an object and b is a member of that object.

즉, 이 경우 a 는 pointer 

(3) Double Colon (Scope) operator is used in namespace related direct member selection scenarios.

print(a::b)

Here, we are accessing b which is a member of the class/namespace a.So, primarily, a is a class/namespace and b is a member (function/ variable etc) of a.


즉, 이 경우 a 는 class/namespace. 

댓글 없음:

댓글 쓰기