What is the meaning of the difference between & # 8594; and. in c / c ++?

Possible duplicates:
C ++: ptr-> hello (); / * VERSUS * / (* ptr) .hello ();
Why does C have a distinction between β†’ and also.?

I know the difference between a member operator (.) And a member using a pointer operator (->).

Why did C designers create another statement for this? Why can't the compiler figure it out on its own?

If you have always used a. Is there any case, if it is ambiguous, do you mean a member or member by a pointer?

edit: I am not looking for the syntax "(* a) .b". I ask why the designers did not allow you to use "ab" instead of "a-> b"?

+7
c ++ c standards
source share
7 answers

If you have a language that is inherently intended for "portable assembler", you are not trying to hide implementation details from the user. Of course, the compiler can understand that in the expression ab , the a question is a pointer. You can? For sure? All time? In a hairy, complex code? And can you imagine the circumstance when it is not possible to quickly notice that a is a pointer, can be a problem?

Stop thinking in terms of β€œhard to write,” and start thinking in terms of β€œeasy to read.” (Yes, I know. This contradicts all programmers-programmers!). You will read the code several orders of magnitude more often than you will write.

0
source share

Do you really want the compiler to figure it out on your own? If so, then the following will evaluate the same thing (assuming p is a pointer to a structure with an element x ):

 (*p).x; px 

If dereferencing a pointer here is implicit, should it be implicit everywhere? Consider:

 int* p; int i = p; // should dereferencing be implicit here as well? 

I think that would be much more confusing than having two separate statements.

It is also useful to have two operators to keep track of how many layers of indirection you have. Of course, the operator -> not needed for this, since p->x equivalent to (*p).x , but it makes the code more understandable and understandable.

+6
source share

Yes, ambiguity arises from the ability to override both operators. The best example is generic pointers . You can use a generic pointer. and β†’ and both have different meanings.

Using β†’ you get access to the object pointed to by. you get access to members of the shared index.

For example:

 class MyObject { public: void myFunction() {} }; boost::shared_ptr<MyObject> ptr; ptr.use_count(); // calls use_count, which is a member of boost::shared_ptr. ptr->myFunction(); // calls MyObject::myFunction, as the operator-> is overriden 
+2
source share

C ++ allows you to override the operator -> . When you have a class of smart pointers, for example std::auto_ptr , the same variable can support as . , and -> for which you use . for access to members auto_ptr and -> for access to members the pointer that it contains.

0
source share

(*a).b matches a->b

-one
source share

Both fo->bar and (*fo).bar do the same!

The language simply provides an operator for indirect access, namely the arrow ( -> ) and treats it as a single character.

-one
source share

The β†’ operator is just an easier way to refer to an object method or element by its pointer, instead of using something like (* p) .member or (* p) .method.

Example:

MyClass * MyObj;

MyObj-> method (); better read and understand than (* MyObj) .method ();

-one
source share

All Articles