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
how do i write a c++ program that will input a number and output its factorial using the while loop

Binomial coefficients are the numeric factors of the products in a power of a binomial such as (x + y)n. For example, (x + y)2 = x2 + 2 x y + y2 has the coefficients 1 2 1. Binomia

Write a C function to solve the system of linear equations A x = y where A is an N by N matrix in the format of pointer-to-pointers and y is a vector in the format of a pointer. Th

Build a program that calculates a discount for items based on quantity bought each item is $99.99 Ask user to enter the quantity of the items Get the quantity of items from the u

padovan string for a natural number is defined

C Program for FIND EVEN & ODD NO.S IN THE ARRAY #include stdio.h> #include string.h> #include conio.h> void main() {           int i=0,j=0, l_e[100],l_o[100];

Within software engineering, software is very rarely written by a single software engineer. Usually a team of engineers write a program; therefore a formal structural design approa

Optimize and Profile a C++ library for speed Project Description:  Optimize and Profile an existing C++ library for speed. This library uses OpenMP and OpenCV and C++ stan

There are 25 employees in a business. Each employee has 5 sales each day of the month (Assume there are 30 days in each month of the year). Draw a flowchart for a pr