operator ++ overloading

B

Bo Sun

hi:

in the following code:

class plus{

int data_item;

public:

plus(int val){ data_item = val;}
plus operator++(){
data_item ++;
return *this;
}

plus operator++(int val) {

data_item += val;
return *this;

}
};

....

plus a(10), b(20);

++a;
b++;

I have the following questions:

1) why ++a will invoke plus operator++()? why b++ will invoke plus
operator++(int val)?

2) in the overloading of operator ++(int val), val is not used. Suppose I
want to make data_item increase by val, like the definition of ++(int
val), how can I operate on b? b++(10) does not work.

many thanks,

bo
 
J

Jeff Schwab

Bo said:
hi:

in the following code:

class plus{

int data_item;

public:

plus(int val){ data_item = val;}
plus operator++(){
data_item ++;
return *this;
}

plus operator++(int val) {

data_item += val;
return *this;

}
};

...

plus a(10), b(20);

++a;
b++;

I have the following questions:

1) why ++a will invoke plus operator++()? why b++ will invoke plus
operator++(int val)?

That's just the way the language is defined. Passing an int parameter
is a real hack, useful only for differentiating between pre- and post-
operators. The int isn't used for anything.
2) in the overloading of operator ++(int val), val is not used. Suppose I
want to make data_item increase by val, like the definition of ++(int
val), how can I operate on b? b++(10) does not work.

Why, that's so crazy it just might work... Try this:

b.operator ++ ( 10 );

Here's some sample code to lead the way; by the way, just for the
record, I hope you are doing this only as a novelty. I mean, this is a
really ugly contortion of C++.

/* I'm going to Hell for this.
*/
#include <iostream>

struct C
{
C operator ++ ( int val )
{
C old_value = *this;

std::cout << val << std::endl;

// ... alter *this ..

return old_value;
}
};


int main( )
{
C c;

c.operator ++ ( 10 );
}
 
D

Dave

Bo Sun said:
hi:

in the following code:

class plus{

int data_item;

public:

plus(int val){ data_item = val;}
plus operator++(){
data_item ++;
return *this;
}

plus operator++(int val) {

data_item += val;
return *this;

}
};

...

plus a(10), b(20);

++a;
b++;

I have the following questions:

1) why ++a will invoke plus operator++()? why b++ will invoke plus
operator++(int val)?
This is just by convention. When C++ was being created, they had to come up
with some way to differentiate between the prefix and postfix forms of
operator++ (and operator-- too for that matter), so this is the way they did
it. The convention is what you've described in question 1: the prefix form
takes no parameters and the postfix form takes one int parameter.

So, for the postfix form, where does the parameter come from and what is its
value? The answer is that the compiler supplies it (unbeknownst to you) and
it has a value of 0. So, when the compiler sees something like a++, it
calls operator++ and passes an int argument of value 0, and so the postfix
form gets called since it takes an int parameter. For the expression ++a,
no argument would be passed behind-the-scenes by the compiler, so the prefix
form would get called since it takes no parameters.
2) in the overloading of operator ++(int val), val is not used. Suppose I
want to make data_item increase by val, like the definition of ++(int
val), how can I operate on b? b++(10) does not work.

The reason this does not work is because the postfix form of operator++
takes one parameter - the hidden parameter I've mentioned above. In the
b++(10) call you show, there are actually two parameters - the hidden one
AND the value of 10 you're trying to pass. There is no version of
operator++ that takes two parameters, so this line does not compile. You
*could* do the following (but I'll explain below why you shouldn't):

b.operator++(10); // Call the operator using function call syntax

This would give you the result you want, but...

operator++ (and operator--) should only be used to increment (decrement) by
1. This is because that is the established convention in C++. To do
otherwise would make your class very confusing to anybody trying to use it.
Everybody expects an increment (decrement) by 1, so to do otherwise would
really throw people off.

To do what you desire, I suggest overloading operator+=.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,413
Latest member
KeiraLight

Latest Threads

Top