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
Question 1. Explain Briefly about class and objects. 2. Create a class string which stores a string value. Overload ++ operator which converts the characters of the string t


1. When developing this project in a Win32 Console Applications that includes the precompiled headers, enter in the Name: box, PRJ2[Your Full Last Name][Your First Initial] with no

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

Define the Data Type Qualifiers in c Language? 1. const 2. volatile const: The const qualifier is used to tell C that the variable value can't change after initialization.

Greatest Common Divisor (GCD) - The greatest common divisor (GCD) of two integers is the largest integer that will evenly divide both integers. The GCD algorithm involves intege


Implementation of the Stack class in C++: How to implement stack class in c++. int Stack::push(int elem) {    if (top    {       list[top++] = elem;       r

Consider text comprised of sentences and sentences comprised of words. Words in a sentence will be space delimited. Given a text and K strings, task is to find out the number valid

palindrome program with minimum character replacements