operator overloading ++

operator overloading for ++ postfix is:

operator++(int x)

eventhough we r not passing int arg "Obj++" how is the post incriment invoked..?

I'm not sure what you mean by asking how it is invoked, but you have a dummy argument to differentiate the signature for postfix from prefix increment.
i mean if der is a class defined which have a operator overloading for preincriment and post incriment

class Incrment
{
...
int count;
public:
Incrment operator++()
{
cout<<"insid++: "<<endl;
this->count++;
return *this;
}

Incrment operator++(int x)
{
cout<<"in agr++: "<<x<<endl; //value of x printed as 0 though we r not
this->count++; //passing any value
return *this;
}

};

main()
{
Incrment I;

++I;//will call fun with out overloading

I++;//will call func with overloading here we r not passing any arg;
}

when only "Incrment operator++();" is declared if i try I++ i ll get a error and same error is observd wen ++I is called with only "Incrment operator++(int x)"
declared.

how will it recognize when to call "operator++();" or "operator++(int x)" as we r not passing any arg for ++I or I++.

is the prototype of the pre and post increment operator overloading predefined in the compiler.


I don't understand how you're confused, you just explained how it works.
Like Zhuge said it's a dummy value. It's not used in the body of the operator overload, it's just to make a difference between pre- and postfix.

how will it recognize when to call "operator++();" or "operator++(int x)" as we r not passing any arg for ++I or I++.

In the case of ++I, the compiler knows it has to call operator++(), for I++ it calls operator++(int).
here is the reason for my confusion i am thinking(not sure) that for post incriment the agument intx x is a default agrument i.e:

Incrment operator++();//for preincriment

Incrment operator++(int x=0);//post incriment


This is not a normal condition to exist:
void show();
void show(int x=0);
this throws an error


even if it exists there is a ambiguty situation when function is called with no argument.
It's a special corner case in C++, you can't honestly expect a 100% perfect language, can you?
for post incriment the agument intx x is a default agrument

It isn't:
1
2
x.operator++(); // calls pre-increment
x.operator++(42); // calls post-increment 

x++ is just compiled as x.operator++(0)
There is such a book as "The Design and Evolution of C++" by Bjarne Stroustrup (the only book of Stroustrup that I advice to read) where he described the history of introducing the syntax for prefix and postfix decrement and increment operators.
At first in C++ there is no difference of postfix and prefix operators. Usually they were defined as

1
2
3
4
5
class A
{
//
   void operator ++();
}; 


So these statements were equivalent

1
2
3
A a;
a++;
++a;


To distinguish postfix and prefix operators it was suggested the following syntax

1
2
3
4
5
class A
{
   A & operator prefix++();
   A operator postfix++();
};


But it required to introduce two new keywords.

Another alternative was

1
2
3
4
5
class A
{
   A & ++operator ();
   A operator++();
};


At last it was decided to use an additional dummy parameter for the postfix operator. So when the compiler sees code something as

a++;

it would know that it has to substitute it to

A operator ++( int );

So when the compiler sees an expression like
++a;

it knows that it must substitute it for

A & operator ++();

and when it sees an expression like

a++;

it knows that it must use

A operator ++( int );

Last edited on
Thank u very much guys..!!
Topic archived. No new replies allowed.