-3

I heard -> is a binary infix pointer member access operator. Now can you explain why does it call so . And what the phrase binary infix mean in this field.

  • 1
    Why would you want to overload an operator you seem to know nothing about? – Scott Hunter Jul 03 '19 at 17:16
  • You could say that `->` is a nullary operator. Although, it does have the implicit object argument so if you count that, then it is unary. The arity of a member function depends on your point of view. – eerorika Jul 03 '19 at 17:17
  • Unary and binary operators, per the standard, can be implemented as non-member functions with one or two parameters respectively. `operator->` must be a non-`static` member and can't be a non-member function. So I'm not sure it qualifies as either. – François Andrieux Jul 03 '19 at 17:19
  • You should limit your posts to a [single question](https://meta.stackexchange.com/questions/222735/can-i-ask-only-one-question-per-post). – François Andrieux Jul 03 '19 at 17:22
  • #Scott Hunter - I post the question to know about. If I already knew about it, I would not do so. I know that -> is a operator that is used with class Or structure type. I thought that may be a binary operator. But i read a book where it was overloaded as unary operator, that's why I post it here. I google d but unable to find a suitable ans –  Jul 03 '19 at 17:25
  • Really seems like a binary operator to me, but yeah I wasn't able to find an exact answer on google either. It's an operator that takes 2 arguments: an object, and an offset (the object member). I don't see any better way of explaining it. – MPops Jul 03 '19 at 17:29
  • 1
    From a layman's point of view, it feels like a binary operator -- you need something on the LHS and you need something on the RHS. I am not sure how it is from a language lawyer's point of view. – R Sahu Jul 03 '19 at 17:29
  • 1
    @S.M.TusharIbneSalam Unfortunately some other user removed the duplicate I've given you. You'll find your answer at [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) and see it doesn't fall in either category of _binary_ or _unary_ operators. – πάντα ῥεῖ Jul 03 '19 at 17:30
  • 1
    #MPops- i also thought like you, that -> is a binary operator as you described. But I got surprised when I saw that it is overloaded as it is a unary operator . And that's why I wanted to knew what kind of operator -> is . If -> would binary operator then I want to eagerly know why it was overloaded as unary operator. Or it is work as both unary and binary operator like unary - and binary - –  Jul 03 '19 at 17:35
  • @S.M.TusharIbneSalam BTW use `@` to ping back anyone from comments here, not `#`. – πάντα ῥεῖ Jul 03 '19 at 17:37
  • @πάντα ῥεῖ Ok, actually this is my first post, i am getting into it, and I read your reference site. There -> is told infix binary operator. I didn't know what infix binary mean. But I googled and read it in "qoura" but I could not understand what is it, and other things that were told in your given site, something about recursive call that also was mystery to me –  Jul 03 '19 at 17:50
  • @πάντα ῥεῖ should I delete the post, if you people think that is not a good question –  Jul 03 '19 at 17:52
  • @S.M.TusharIbneSalam You may want to [edit] your question and elaborate what exactly you don't understand. – πάντα ῥεῖ Jul 03 '19 at 17:52
  • 1
    @S.M.TusharIbneSalam "Infix" just means the operator is written between two operands, e.g. `a + b` (as opposed to weird notations like `+ a b` and `a b +`). – HolyBlackCat Jul 03 '19 at 18:09
  • The C++ precedence table refers to first and second argument, making it binary. https://en.cppreference.com/w/cpp/language/operator_member_access#Built-in_member_access_operators – stark Jul 03 '19 at 18:10

1 Answers1

2

Straight from wikipedia (https://en.wikipedia.org/wiki/Infix_notation):

Infix notation ... is characterized by the placement of operators between operands

Again from wikipedia (https://en.wikipedia.org/wiki/Binary_operation):

... a binary operation ... is a calculation that combines two elements (called operands) to produce another element.

In c++ an expression in the [simplified] form expr -> id-expr is called a member access operator expression. The built-in version provides access to a data member or member function of the class pointed-to by the pointer operand.

Since it takes two expressions to form a member access operator expression we might say that it is binary, and since the symbol denoting the operator is placed between the two expressions we might say that it is infix.

cff
  • 61
  • 2
  • The right-hand "operand" is barely an expression, though. Grammatically it is an *id-expression*, but instead of being evaluated it is merely looked up (and possibly subjected to overload resolution). It therefore has to have higher precedence than any binary operation (consider `p->i++`), and the standard terms it a **postfix** expression. – Davis Herring Feb 25 '23 at 08:15