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

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];

...

}

 

Posted Date: 3/20/2013 3:45:32 AM | Location : United States







Related Discussions:- What operators can or cannot be overloaded?, Assignment Help, Ask Question on What operators can or cannot be overloaded?, Get Answer, Expert's Help, What operators can or cannot be overloaded? Discussions

Write discussion on What operators can or cannot be overloaded?
Your posts are moderated
Related Questions
Need FTP Upload and MySQL Project Description: We are in need of an executable application that will be execute as a scheduled task on Windows Server 2008 R2 and can perform


write a program to solve e^x

Question: (a) Explain a linked list. (b) Describe the three different types of linked list with the help of diagrams. (c) Give two advantages and two disadvantages

This is what I need help with. "Create a program creates an interface allowing the user to select from some other programs I have made one being a money converter and the other ca

write a class player that contain attributes for the player,s name,average and team

Should my constructors employ "assignment" or "initialization lists"?

WAP TO PRINT SERIES FROM 1 TO 10 & FIND ITS SQUARE AND CUBE # include stdio.h> # include conio.h> # include math.h>   void main () { int a=1,sqr=0,cube=0;

Inline Functions Imagine a c program, which reads disk records having employee information. If this is a payroll application each employee record data is probably processed by

We need an E Commerce site likefifacointrader. you must have experience with that kind of work You have to show a sample of work like that site. Skills required are C Prog