4

is subscript operator [ ] unary or binary operator ?

I'm quite new to C++ and was going through operator operloading and wondered is subscript a unary or binary?

2 Answers 2

5

The subscript operator is a binary operator in the strict sense as it takes two arguments, the reference to the object and the value.

int arr[3];

Here you can see that [] operator makes use of both arr and 3.

Sign up to request clarification or add additional context in comments.

5 Comments

This has been already discussed here: stackoverflow.com/questions/4302142/subscript-operator-postfix Read this for a detail explanation
Yes, that's what i thought .
what do you wanna say about '->' pointer to member operator ?
-> is also a binary operator. For more on the operators, check this site : codingunit.com/unary-and-binary-operator-table
Since C++23 subscript operator can accept several indices, say, two. Does it make it (in case when two indexes are accepted) ternary operator?
1

According to the C++ Standard

13.5.1 Unary operators

1 A prefix unary operator shall be implemented by a non-static member function (9.3) with no parameters...

and

13.5.2 Binary operators

1 A binary operator shall be implemented either by a non-static member function (9.3) with one parameter...

Thus the subscript operator is a binary operator.

The Unary operators in C++ are:

unary-operator: one of
    * & + - ! ~

and also you may add to unary operators

++ cast-expression
-- cast-expression

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.