Einzelnen Beitrag anzeigen

Benutzerbild von Khabarakh
Khabarakh

Registriert seit: 18. Aug 2004
Ort: Brackenheim VS08 Pro
2.876 Beiträge
 
#5

Re: [C++] Pointer-Problem: illegal indirection

  Alt 7. Nov 2007, 19:45
Der bislang völlig von C++ Verschonte wagt eine Antwort, mal sehen, was daraus wird . Jedenfalls dürfte in C++ wie in C#[*] gelten, dass "*" als unärer Operator einen niedrigeren Rang als "." als primärer Operator einnimmt. Solange du also nicht den Rückgabewert der Methode dereferenzieren willst, musst du klammern oder "->" verwenden.
Zitat:
postfix-expression . name
postfix-expression –> name


Remarks
The member access operators . and -> are used to refer to members of structures, unions, and classes. Member access expressions have the value and type of the selected member.

There are two forms of member access expressions:

In the first form, postfix-expression represents a value of struct, class, or union type, and name names a member of the specified structure, union, or class. The value of the operation is that of name and is an l-value if postfix-expression is an l-value.

In the second form, postfix-expression represents a pointer to a structure, union, or class, and name names a member of the specified structure, union, or class. The value is that of name and is an l-value. The –> operator dereferences the pointer. Therefore, the expressions e–>member and (*e).member (where e represents a pointer) yield identical results (except when the operators –> or * are overloaded).
[*] Bitte daraus nicht schließen, dass man in c# solche Ungeheuer öfter als zweimal pro Dekade bräuchte ^^ .

PS: Dass der Compiler den nicht-passenden Typen überhaupt nicht beim Namen nennen, könnte man ja fast als blanken Hohn ansehen... aber mit seinen knapp 30 Jahren kann man das der Sprache wohl noch verzeihen .

[edit]
@Torpedo: So wie ich mein Zitat verstanden habe, kann der .-Operator nur nach dem Dereferenzieren der Klasse benutzt werden - sonst wäre sein "->"-Kumpane ja völlig witzlos.
[/edit]
Sebastian
Moderator in der EE
  Mit Zitat antworten Zitat