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..?

1

2 Answers 2

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

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

Comments

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.

5 Comments

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<<v[i] ? what if i have to modify them say multiplu all the elements by 2 ?
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].
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: stackoverflow.com/questions/388242/…
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. :)
Click the Badges button at the top of the page, or go here: stackoverflow.com/help/badges. Then read the FAQ. Welcome to Stack Overflow.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.