Explanation of the code snippet , keyword operator in , in c++ , use of objects in classes [closed]

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++ prog. language 4th edition , Bjarne
IDE : Microsoft Visual Studio Professional 2013
My experience : Newbie , have learned a little using turbo c++ before.

I noticed while typing function2 that operator is a keyword(shown in blue color) , but when whole function2 is declared , operator turns white , that is it doesn’t remain an keyword anymore.

If i try using any other name other than “operator” , an error pops up , which says
Error: array of function is not allowed

It would be very helpful if someone can explain what is happening in function2, I want to know all that there is about the keyword operator , why,how and when to use this.Ofcourse from a beginner’s perspective.

Please refrain from overly complex answers , I am complete newbie

I tried searching on internet , 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..?

Basically it is out of my knowledge , please explain line3 in simplest possible manner, and at the same time as exhaustively as possible , so that I can learn.

PS: Do not tell me , that i shouldn’t be learning c++ , or i shouldn’t be using that IDE or anything else like that. If you can , then answer the question, I shall be very thankful.

3

What this is is called operator overloading. You are overloading the operator [] with a new method. So basically, operator is a conditional keyword here. You can’t use it as a method or variable name, but you can reference it for defining an operator overload. So its reserved in some situations, but not others, like the above example.

And to explain operator overloading: You are adding operators to a class so that it can do that functionality when you use the specific syntax associated with that operator.

So, in this case, we’re adding the array access operator to the class, so we can use that functionality as if it were an array.

Examples of operators you can overload:

  • addition (+)
  • subtraction (-)
  • multiplication (*)

and so on.

In essence, operator overloading allows a class to act like another type of object. Any sort of operation, like addition, assignment, increment, decrement, comparisons(<, >, !=, ==, etc), and so on all are operators that can be overloaded. This allows you to write a class and be able to use that syntax, such as newObj > oldObj, instead of calling a method, like newObj->IsGreaterThan(oldObj).

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật