0

Can I have two overloaded operator[] like this in the same class?

I am confused which definition is used when I use operator[], isn't int ambiguous? Don't they have the same signature?

template <class T, int n> 
class ArrayTP 
{ 
private: 
    T ar[n]; 
public: 
    ArrayTP() {};

    virtual T & operator[](int i); 
    virtual T operator[](int i) const;
};

This class contains declarations of these overloaded operator. I have not included definition in my question, though.

Aayush Neupane
  • 1,066
  • 1
  • 12
  • 29
  • Does this answer your question? [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) – Retired Ninja Nov 12 '19 at 02:52
  • 1
    "_don't they have same signature?_" No. One is `const` method, while other is not. – Algirdas Preidžius Nov 12 '19 at 02:54
  • 2
    In a `const` method `this` is `const`. This isn't exactly what's going on, but it should be close enough to show you the difference: `T & operator[](int i)` is sort of `T & operator[](ArrayTP * this, int i)` and `T operator[](int i) const` is sort of `T operator[](const ArrayTP * this, int i)` – user4581301 Nov 12 '19 at 03:01

1 Answers1

1

Overloaded operators works no different than normal overloaded functions. It's just they are special function. So I'm giving you general example from function same applies to any kind of functions.

As you must know that, top-level const has no effect on the objects that can be passed to the function. A parameter that has a top-level const is indistinguishable from one without a top-level const:

Record lookup(Phone);
Record lookup(const Phone); // redeclares Record lookup(Phone)
Record lookup(Phone*);
Record lookup(Phone* const); // redeclares Record lookup(Phone*)

In these declarations, the second declaration declares the same function as the first. On the other hand, we can overload based on whether the parameter is a reference (or pointer) to the const or nonconst version of a given type; such consts are low-level.

// functions taking const and nonconst references or pointers have different parameters 
// declarations for four independent, overloaded functions
Record lookup(Account&); // function that takes a reference to Account
Record lookup(const Account&); // new function that takes a constbreference.
Record lookup(Account*); // new function, takes a pointer to Account
Record lookup(const Account*); // new function, takes a pointer to const

In these cases, the compiler can use the constness of the argument to distinguish which function to call. Because there is no conversion from const, we can pass a const object (or a pointer to const) only to the version with a const parameter. Because there is a conversion to const, we can call either function on a nonconst object or a pointer to nonconst. However, the compiler will prefer the nonconst versions when we pass a nonconst object or pointer to nonconst. Examples from primer.

Nikhil Badyal
  • 1,589
  • 1
  • 9
  • 20
  • in my question we always use integer to access the element of an array so what is the need of two [] operator declaration? – Aayush Neupane Nov 12 '19 at 04:02
  • 1
    @AAYUSHNEUPANE one operator allows read/write access to the values stored in a non-const ArrayTP object. The other operator allows read-only access to the values stored in a const ArrayTP object – Remy Lebeau Nov 12 '19 at 07:02
  • Ofc ! As one, is of const type so it won't let you write through it. Well i have a suggestion to go through a good book. https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Nikhil Badyal Nov 12 '19 at 10:25