What operators can or cannot be overloaded?, C/C++ Programming

Assignment Help:

A: Mostly can be overloaded. The only C operators which can't be are. and?: (and sizeof, that is technically an operator). C++ adds a few of its own operators, mostly which can be overloaded except :: and .*.

Here's an instance of the subscript operator (it returns a reference). Primary without operator overloading:

class Array {

public:

int& elem(unsigned i) { if (i > 99) error(); return data[i]; }

private:

int data[100];

};

 

int main()

{

Array a; a.elem(10) = 42; a.elem(12) += a.elem(13);

...

}

Now the similar logic is presented along with operator overloading:

class Array {

public:

int& operator[] (unsigned i) { if (i > 99) error(); return data[i]; }

private:

int data[100];

};

int main()

{

Array a; a[10] = 42; a[12] += a[13];

...

}

 


Related Discussions:- What operators can or cannot be overloaded?

#c++ homework, write c++programm that converts degrees Kelvin (TK) to degr...

write c++programm that converts degrees Kelvin (TK) to degrees Fahrenheit(TR) recall that TF=(9/5)TK and that TF=TR-459.67

Define namespace in c++, It is a feature in C++ to reduce name collisions i...

It is a feature in C++ to reduce name collisions in the global name space. This namespace keyword assigns a separate name to a library that allows other libraries to use the simila

#padovan string, #padovan string   program in java // aakash , sur...

#padovan string   program in java // aakash , suraj , prem sasi kumar kamaraj college program 1 : package test.padovanstring; public class PadovanString {

Write a program of inline function, Here is a program that uses an inline f...

Here is a program that uses an inline function to compute and return the absolute value of its input argument. # include inline int abs(int x) {                  ret

Explain operators in c language, Explain Operators in C Language? The C...

Explain Operators in C Language? The C language is very rich in built - in - operators and it places more significance on operators than do most other computer languages. The C

Explain brief about class and objects, Question 1 Write a program that acc...

Question 1 Write a program that accepts a 3x3 matrix from the user and finds the transpose of it Question 2 Explain Brief about class and objects. Also describe friend functi

Decodethecode, 6999066263304447777077766622337778 -----> message sent by th...

6999066263304447777077766622337778 -----> message sent by the first smuggler. my name is robert---------> message decoded by the second smuggler. Where ‘0’ denotes the "space".

Last fibonnaci standing, i need a program for finding the last fibonacci nu...

i need a program for finding the last fibonacci number

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd