0
class Vector { 
      public: 
       Vector(int s) :elem{new double[s]}, sz{s} { }
       double& operator[](int i) { return elem[i]; } //function 2
       int size() { return sz; } 

        private: 
            double∗ elem;  
            int sz; 
     };

Code snippet from: The C++ Programming Language 4th edition, Bjarne Stroustrup
IDE : Microsoft Visual Studio Professional 2013

Please explain what does the operator keyword do? I tried searching couldn't find anything else apart from operator overloading, which is not my question

double read_and_sum(int s) { 
                    Vector v(s);                     //line 1
                    for (int i=0; i!=v.size(); ++i) 
                    cin>>v[i];                        //line 3
                    double sum = 0; 
                    for (int i=0; i!=v.size(); ++i) 
                    sum+=v[i];
                     return sum;
                    }

Here line1 passes the argument "s" of int type needed by constructor of class vector , that's fine.

But in line3 how can the statement "cin>>v[i]" be valid ? , since the object v isn't declared as an array of objects. Even if it is valid where does the value go..?

Mann
  • 1
  • 5
  • Since it was not mentioned in the answers: [Overloaded operators are functions with special function names](https://en.cppreference.com/w/cpp/language/operators). In your example expression `v[i]` is equivalent to `v.operator[](i)` – pptaszni Mar 31 '20 at 16:36

2 Answers2

1

Seems like you are new to C++. The following links about operator overloading might be useful.

Basics of operator overloading: Operator Overloading.

Full list of operators that can be overloaded: Operator Overloading

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

I tried searching on internet, couldn't find anything else apart from operator overloading, which is not my question.


Actually it seems that your question is about operator overloading, you just don't know it.

The operator keyword is used in a class declaration to overload an operator. In the example that you've shown, the class is overloading the array index operator []. This is what allows the syntax cin>>v[i] (the second part of your question) to work. Because the [] operator is overloaded, you can reference the class object in this way - when you make such a reference, the compiler generates code to call that overloaded definition. The overloaded definition tells it how to get the right object, given the array index. This lets your class act like an array, even though it really isn't one.

Operator overloading is often misunderstood and often misused, but when used properly it is a uniquely powerful feature of C++.

I can't answer the part about the keyword coloring in Visual Studio - sounds like it's just a peculiarity of the Microsoft editor. But operator is definitely a keyword in C++, regardless of how the IDE shows it.

Jeff Loughlin
  • 4,134
  • 2
  • 30
  • 47
  • so the cin>>v[i] , gives the value to elem ,which is pointing to array of doubles ? , if for example i after storing the values , i want to access them , i would just write cout< – Mann Mar 27 '14 at 17:37
  • A properly overloaded [] operator should allow you to write code that treats your object just like an array. So yes, you should be able to write stuff like x = v[i], v[i] = n, or v[i] = v[j]. – Jeff Loughlin Mar 27 '14 at 17:42
  • The Bjarne Stroustrup book is NOT a good place to learn C++. It's a wonderful reference, but there are much better books to learn from. Here's a link with a lot of C++ book recommendations: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Jeff Loughlin Mar 27 '14 at 17:50
  • Thank you very much @Jeff Loughlin , accelerated c++ seems the right one for me , at the risk of being opportunistic i would like to know about the different badges and rep scores here , i just received a scholar badge. :) – Mann Mar 27 '14 at 17:56
  • Click the Badges button at the top of the page, or go here: http://stackoverflow.com/help/badges. Then read the FAQ. Welcome to Stack Overflow. – Jeff Loughlin Mar 27 '14 at 17:58