473,654 Members | 3,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9824
On 2007-11-11 17:10:07 -0800, "je**********@g mail.com"
<je**********@g mail.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*********@Nn OwSlPiAnMk.comw rote in
news:2007111117 592211272-NpOeStPeAdM@NnO wSlPiAnMkcom:
On 2007-11-11 17:10:07 -0800, "je**********@g mail.com"
<je**********@g mail.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...@Nn OwSlPiAnMk.comw rote:
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*********@Nn OwSlPiAnMk.comw rote in
news:2007111119 444827544-NpOeStPeAdM@NnO wSlPiAnMkcom:
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**********@g mail.comwrote in message
news:11******** *************@5 0g2000hsm.googl egroups.com...
On Nov 11, 8:44 pm, Peter Duniho <NpOeStPe...@Nn OwSlPiAnMk.comw rote:
>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.no spamwrote:
<jehugalea...@g mail.comwrote in message

news:11******** *************@5 0g2000hsm.googl egroups.com...
On Nov 11, 8:44 pm, Peter Duniho <NpOeStPe...@Nn OwSlPiAnMk.comw rote:
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
3120
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. When I find is that if I send the value by the GET method, response.write("From QueryString: " & Request.QueryString("usernamefromform") & "<br><br>")
1
4771
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 post where i get these values like the amount from the database. eg of the post: <form method="post" action="https://payflowlink.verisign.com/payflowlink.cfm"> <input type="hidden" name="name" value="john adams">
2
12548
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 <form action="display.aspx" method="post" target="_blank"> will submit the form data and open display.asp in a new browser
1
2968
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 used Live Http Headers to retrieve the POST information, I included it at the end of this message. Can anyone tell me how to make THIS kind of post? Another thing, "Content-Type: multipart/form-data;
10
3418
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 database or continue to process on to the next page. I am now trying to learn ASP to see if we can replace some of our applications that were written in php with an ASP alternative. However, after doing many searches on google and reading a couple...
9
3085
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 expertise. That save me a lot of time and stress. I want to rate the post, but I only see questions "is this post helpful" and " why should I rate a post" link. I didn't see any thing "rate the post" link. I already sign into the community...
3
4909
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 one post for every 10 seconds the client start getting error 403 'forbidden' from the webserver after a short period of time. The webserver is IIS. The choking of the client/server communication when doing high frequency posting is due to that we...
6
5097
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 how to encode a file on a POST to the same web service. The definition requires a base64binary encoded file, which I have tried. The form is also using a mutlipart/form-data enctype, but I either get a 500 error or 'Request format is invalid'. ...
10
15689
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 this encoding is required in the case of a GET request and the form data is attached as a URI query string; however, why is the encoding necessary for POST requests? Thanks,
0
8379
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8294
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8709
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8494
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8596
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7309
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.