But operator overloading makes class look ugly; isn''t it , C/C++ Programming

Q: But operator overloading makes class look ugly; isn't it assumed to make my code clearer?

A: Operator overloading makes life simpler for the users of a class, not for developer of the class!

Assume the following example.

class Array {

public:

int& operator[] (unsigned i); // Some people don't like this syntax

...

};

inline

int& Array::operator[] (unsigned i) // Some people don't like this syntax

{

...

}

Some programmer doesn't like the keyword operator or the somewhat humorous syntax which goes with it in the body of the class itself. However the operator overloading syntax isn't imagined to make life simpler for the developer of a class. It's imagined to make life simpler for the users of the class:

int main()

{

Array a;

a[3] = 4; // User code should be obvious and easy to understand...

...

}

Remember: in a reuse-oriented world, usually there will be many people, who employ your class, although there is only one person who builds it (yourself); thus you should do things that favor the several instead of the few.

 

Posted Date: 3/20/2013 3:44:51 AM | Location : United States







Related Discussions:- But operator overloading makes class look ugly; isn''t it , Assignment Help, Ask Question on But operator overloading makes class look ugly; isn''t it , Get Answer, Expert's Help, But operator overloading makes class look ugly; isn''t it Discussions

Write discussion on But operator overloading makes class look ugly; isn''t it
Your posts are moderated
Related Questions
program for factorial

Write a program that predicts users’ age (0-128 years old) with at most 7 questions. The game starts with asking the user whether he/she is younger or older than G (an initial gues

# include stdio.h> # include conio.h> # include string.h> void main()   {           int i=0,j=0;           char a[100],temp;           clrscr();

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b.

overloadstream insertion opertator to display the data of object on the console

Stack operations - C++: Write a program to define basic stack operations in c++. int Stack::push(int elem) {    int m = getmax();    if (top    {       put_

find wether specific word is inside a pool of word and what is the direction e.g. north


Problem Statement: You have to write a C program to develop a Car Parking Management System for a busy commercial area. The system will record the car plate number, date and time w

A more advanced data type is the structure; here we can define a template as a collection of different variables e.g.     struct birthdate   {     int month;     int day;