472,127 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

Post-Increment and Pre-Increment Overloading

Hello:

In C++, you had to distinguish between post and pre increments when
overloading. Could someone give me a short demonstration of how to
write these?

I get the impression that are handled with the same overload, but I
just want to make sure.

Nov 12 '07 #1
13 9648
On 2007-11-11 17:10:07 -0800, "je**********@gmail.com"
<je**********@gmail.comsaid:
Hello:

In C++, you had to distinguish between post and pre increments when
overloading. Could someone give me a short demonstration of how to
write these?
How to write which? The C++ versions? Or the C# version?
I get the impression that are handled with the same overload, but I
just want to make sure.
As far as I know, your impression is correct. There is a single ++
operator you can overload. Until you brought up the C++ behavior, I
never even gave much thought to the possibility it might be otherwise.
:)

See http://msdn2.microsoft.com/en-us/library/8edha89s.aspx for more
details on which operators can be overloaded and how to go about it.

Pete

Nov 12 '07 #2
Peter Duniho <Np*********@NnOwSlPiAnMk.comwrote in
news:2007111117592211272-NpOeStPeAdM@NnOwSlPiAnMkcom:
On 2007-11-11 17:10:07 -0800, "je**********@gmail.com"
<je**********@gmail.comsaid:
>Hello:

In C++, you had to distinguish between post and pre increments when
overloading. Could someone give me a short demonstration of how to
write these?

How to write which? The C++ versions? Or the C# version?
>I get the impression that are handled with the same overload, but I
just want to make sure.

As far as I know, your impression is correct. There is a single ++
operator you can overload. Until you brought up the C++ behavior, I
never even gave much thought to the possibility it might be otherwise.
:)

See http://msdn2.microsoft.com/en-us/library/8edha89s.aspx for more
details on which operators can be overloaded and how to go about it.

Pete
I don't know about .Net-land, but in native C++, the pre/post-increment
operators are 2 separate operators.

class xyzzy
{
....
xyzzy& operator++() // prefix
{
// todo: increment this
return *this;
}
xyzzy operator++(int) // postfix
{
xyzzy tmp(*this);
// todo: increment this
return tmp;
}
};

Dave Connet
Nov 12 '07 #3
On 2007-11-11 19:37:31 -0800, David Connet <co****@entelos.comsaid:
I don't know about .Net-land, but in native C++, the pre/post-increment
operators are 2 separate operators.
Yes, of course they are. Both the OP and I acknowledged that. And I
suppose if the OP is asking for how to declare such in C++, your post
would be relevant to that question (but not what I'd call on-topic in
this newsgroup).

Nov 12 '07 #4
On Nov 11, 8:44 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
On 2007-11-11 19:37:31 -0800, David Connet <con...@entelos.comsaid:
I don't know about .Net-land, but in native C++, the pre/post-increment
operators are 2 separate operators.

Yes, of course they are. Both the OP and I acknowledged that. And I
suppose if the OP is asking for how to declare such in C++, your post
would be relevant to that question (but not what I'd call on-topic in
this newsgroup).
I was curious about how it is done in C#. I am more than aware of how
to do it in C++.

Thanks for your replies.

Nov 12 '07 #5
Peter Duniho <Np*********@NnOwSlPiAnMk.comwrote in
news:2007111119444827544-NpOeStPeAdM@NnOwSlPiAnMkcom:
On 2007-11-11 19:37:31 -0800, David Connet <co****@entelos.comsaid:
>I don't know about .Net-land, but in native C++, the pre/post-increment
operators are 2 separate operators.

Yes, of course they are. Both the OP and I acknowledged that. And I
suppose if the OP is asking for how to declare such in C++, your post
would be relevant to that question (but not what I'd call on-topic in
this newsgroup).
Sorry - I misread the original post. I thought the OP was asking how to do
it in C++ and C#.

Dave
Nov 12 '07 #6
Lew
David Connet wrote:
Sorry - I misread the original post. I thought the OP was asking how to do
it in C++ and C#.
Your interpretation was not contradicted by anything in the original post.

--
Lew
Nov 12 '07 #7
In C++, you had to distinguish between post and pre increments when
overloading. Could someone give me a short demonstration of how to
write these?

I get the impression that are handled with the same overload, but I
just want to make sure.
Since no one has yet answered your question, yes there is only one overload
for both pre and post-increment.

public static EttMonth operator ++(EttMonth m1)
{
return m1 + 1;
}

This is from some of my code. EttMonth is a struct (represents a month in
yyyyMM integer form). The + operator is also overloaded to add the number of
months specified by the RHS.

Chris

Nov 12 '07 #8

<je**********@gmail.comwrote in message
news:11*********************@50g2000hsm.googlegrou ps.com...
On Nov 11, 8:44 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
>On 2007-11-11 19:37:31 -0800, David Connet <con...@entelos.comsaid:
I don't know about .Net-land, but in native C++, the pre/post-increment
operators are 2 separate operators.

Yes, of course they are. Both the OP and I acknowledged that. And I
suppose if the OP is asking for how to declare such in C++, your post
would be relevant to that question (but not what I'd call on-topic in
this newsgroup).

I was curious about how it is done in C#. I am more than aware of how
to do it in C++.
C# doesn't have compound assignment operators (like +=, *=, ++). Instead,
it calls the base operator (+, *) and then the compiler generates the
assignment. Prefix and postfix are handled the same way (see section 7.5.9
of the standard). The operator++ implementation is NOT responsible for
changing the value in-place as it is in C++, rather the C# compiler assigns
the value returned from the operator++ implementation as a separate step.

"The run-time processing of a postfix increment or decrement operation of
the form x++ or x-- consists of the following steps:
· If x is classified as a variable:

o x is evaluated to produce the variable.

o The value of x is saved.

o The selected operator is invoked with the saved value of x as its
argument.

o The value returned by the operator is stored in the location given
by the evaluation of x.

o The saved value of x becomes the result of the operation.

· If x is classified as a property or indexer access:

o The instance expression (if x is not static) and the argument list
(if x is an indexer access) associated with x are evaluated, and the results
are used in the subsequent get and set accessor invocations.

o The get accessor of x is invoked and the returned value is saved.

o The selected operator is invoked with the saved value of x as its
argument.

o The set accessor of x is invoked with the value returned by the
operator as its value argument.

o The saved value of x becomes the result of the operation.

The ++ and -- operators also support prefix notation (§7.6.5). The result of
x++ or x-- is the value of x before the operation, whereas the result of ++x
or --x is the value of x after the operation. In either case, x itself has
the same value after the operation.

An operator ++ or operator -- implementation can be invoked using either
postfix or prefix notation. It is not possible to have separate operator
implementations for the two notations."

>
Thanks for your replies.

Nov 12 '07 #9
On Nov 12, 9:55 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
<jehugalea...@gmail.comwrote in message

news:11*********************@50g2000hsm.googlegrou ps.com...
On Nov 11, 8:44 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
On 2007-11-11 19:37:31 -0800, David Connet <con...@entelos.comsaid:
I don't know about .Net-land, but in native C++, the pre/post-increment
operators are 2 separate operators.
Yes, of course they are. Both the OP and I acknowledged that. And I
suppose if the OP is asking for how to declare such in C++, your post
would be relevant to that question (but not what I'd call on-topic in
this newsgroup).
I was curious about how it is done in C#. I am more than aware of how
to do it in C++.

C# doesn't have compound assignment operators (like +=, *=, ++). Instead,
it calls the base operator (+, *) and then the compiler generates the
assignment. Prefix and postfix are handled the same way (see section 7.5..9
of the standard). The operator++ implementation is NOT responsible for
changing the value in-place as it is in C++, rather the C# compiler assigns
the value returned from the operator++ implementation as a separate step.

"The run-time processing of a postfix increment or decrement operation of
the form x++ or x-- consists of the following steps:
· If x is classified as a variable:

o x is evaluated to produce the variable.

o The value of x is saved.

o The selected operator is invoked with the saved value of x as its
argument.

o The value returned by the operator is stored in the location given
by the evaluation of x.

o The saved value of x becomes the result of the operation.

· If x is classified as a property or indexer access:

o The instance expression (if x is not static) and the argument list
(if x is an indexer access) associated with x are evaluated, and the results
are used in the subsequent get and set accessor invocations.

o The get accessor of x is invoked and the returned value is saved.

o The selected operator is invoked with the saved value of x as its
argument.

o The set accessor of x is invoked with the value returned by the
operator as its value argument.

o The saved value of x becomes the result of the operation.

The ++ and -- operators also support prefix notation (§7.6.5). The result of
x++ or x-- is the value of x before the operation, whereas the result of ++x
or --x is the value of x after the operation. In either case, x itself has
the same value after the operation.

An operator ++ or operator -- implementation can be invoked using either
postfix or prefix notation. It is not possible to have separate operator
implementations for the two notations."


Thanks for your replies.- Hide quoted text -

- Show quoted text -
Well that brings me to my next question - does it bother anyone that
overloaded operators operate on a new instance rather than the given
instance? How do unary operators affect the given instance? If the
operation simply assigns a modified version to my instance, how do I
know the assignment worked as expected? Are these questions worded
well enough?

Nov 12 '07 #10
On 2007-11-12 10:19:42 -0800, "je**********@gmail.com"
<je**********@gmail.comsaid:
Well that brings me to my next question - does it bother anyone that
overloaded operators operate on a new instance rather than the given
instance?
You mean for value types, right? For reference types (operator defined
on a class), the reference to the operand is passed by value to the
operator overload. Thus, the overload is using the original instance,
not a new instance.

As for whether the behavior for value types bothers me, the answer is
no. It doesn't bother me at all. In fact, the behavior is quite
consistent with the general use of value types. One characteristic of
value types is that they often wind up being copied as a consequence of
being used in expressions or passed to methods.
How do unary operators affect the given instance?
For reference types, if the instance is modified inside the overload
method, the instance is modified. If a new instance is returned
instead (something you might do for an immutable type), the operator
doesn't affect the given instance.

Note that in either case, you can return something other than the
original passed-in reference, and this will modify the variable storing
the reference being used. But whether the original instance is
modified or not depends on the overload method itself, not the return
value.

For value types, the overload method doesn't affect the given instance
at all since the operand is passed to the overload method by value. To
affect the given instance, the method must return the modified value so
that the code the compiler generates can then copy that back to the
original variable.
If the
operation simply assigns a modified version to my instance, how do I
know the assignment worked as expected?
Why wouldn't it?
Are these questions worded
well enough?
I think the first two questions seem clear enough. I admit, I don't
really understand the third question. I'm sure there's no problem, but
I don't even see the hypothetical problem you seem to be concerned with.

Pete

Nov 12 '07 #11
Peter Duniho <Np*********@NnOwSlPiAnMk.comwrote:
How do unary operators affect the given instance?

For reference types, if the instance is modified inside the overload
method, the instance is modified. If a new instance is returned
instead (something you might do for an immutable type), the operator
doesn't affect the given instance.
Whether or not the type is immutable, it's a bad idea to make operators
have side effects. For instance, if I were to do:

MyCustomType t = new MyCustomType(someArguments);

MyCustomType t2 = -t;

it would be *very* odd if the object referred to by t were changed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 12 '07 #12
On 2007-11-12 11:23:36 -0800, Jon Skeet [C# MVP] <sk***@pobox.comsaid:
Peter Duniho <Np*********@NnOwSlPiAnMk.comwrote:
>>How do unary operators affect the given instance?

For reference types, if the instance is modified inside the overload
method, the instance is modified. If a new instance is returned
instead (something you might do for an immutable type), the operator
doesn't affect the given instance.

Whether or not the type is immutable, it's a bad idea to make operators
have side effects.
I do agree. But the guy asked. :)

Nov 12 '07 #13

hai
in c++ all unary operators are preincrement
while declaring postincrement simplely u give dummy defination i.e
operator++(int)//post increment;
operator++(void);//pre increment
*** Sent via Developersdex http://www.developersdex.com ***
Nov 20 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Thomas Scheiderich | last post: by
1 post views Thread by Manuel | last post: by
10 posts views Thread by glenn | last post: by
9 posts views Thread by c676228 | last post: by
3 posts views Thread by JansenH | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.