473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 9791
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Thomas Scheiderich | last post by:
I am trying to understand Session variables and ran into a question on how they work with data that is passed. I have an HTM file that calls an ASP file and sends the name either by GET or POST....
1
by: khawar | last post by:
my application is in asp.net using C# hi guys having a complicated problem i am using payflowlink to process CC payments I have to send a httppost to their servers. The problem is how do i do a...
2
by: Matt | last post by:
When we submit the form data to another page, we usually do the following: <form action="display.aspx" method="post"> will submit the form data and open display.asp in the current browser ...
1
by: Manuel | last post by:
I have to log into a website and retrieve some information. The problem is that the post isn't "normal". I'm used to passing post values in the form of: Variable1=Value1&Variable2=Value2 etc. I...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
9
by: c676228 | last post by:
Hi, I am new to this discussion forum. I started to post questions on this forum since this Jan. and got many good responses and I am very appreciated to those who are willing to help with their...
3
by: JansenH | last post by:
We have implemented a 'HTTP Post' client in C# that posts Xml documents to a webserver. This is working fine if the post rate is one post for every 20 seconds. But if the post rate is increased to...
6
by: Brybot | last post by:
I am trying to allow HTTP POST file uploads to my web service. Currently I have it working perfectly for a SOAP/XML request reading in a byte using MemoryStream/FileStream but I cannot figure out...
10
by: Peter Michaux | last post by:
Hi, All Ajax libraries I've read use encodeURIComponent() on the name- value pairs extracted from forms before POST ing the result to the server with and xmlhttprequest. I can understand why...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.