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?

I want youtube down-upload boots, Project Description: Boots capture vid...

Project Description: Boots capture video from your YouTube channel and upload your own video to our channel or the copyright of the videos, bang, 3 parties, automatically delete

Library functions, is getchar() is a C++ library function?

is getchar() is a C++ library function?

Command line program to find name matches, What's a six-letter word that ha...

What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than a

Integer variable, how do you declare an integer variable

how do you declare an integer variable

Wap to print sum=1!+2!+3!+4!+....n!, WAP TO PRINT SUM=1!+2!+3!+4!+....N! ...

WAP TO PRINT SUM=1!+2!+3!+4!+....N! #include stdio.h> #include conio.h>   void main() {                    int n,i;                    float sum=0,fac

Padovan sequence, write a program that counts the number of occurrences of ...

write a program that counts the number of occurrences of the string in the n-th Padovan string P(n)

Define classes and objects, Classes and Objects A class is a vehicle to...

Classes and Objects A class is a vehicle to execute the OOP features in the C++ language. Once a class is declared, an object of that type can be explained. An object is said t

Srand and rand(), Mention clearly about srand and rand().

Mention clearly about srand and rand().

Minimum shelf, #At a shop of marbles, packs of marbles are prepared. Packet...

#At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with the

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