473,395 Members | 1,742 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,395 software developers and data experts.

Overloading operator++

Hi,

I want to overload operator++ in my custom iterator class, which I do
like this:

internal struct IListIterator
{
public static IListIterator operator++(IListIterator ii)
{
idx += 1;
// copy at the new position->prefix operator
return new IListIterator(null, idx );
}
}

The question is: Is this the prefix or postfix form? How do both forms
differ in the declaration? I tried the C++ way with an second int as a
parameter, but that of course fails as ++ is an unary operator.

Thanks in advance,
Andy
Nov 15 '05 #1
4 7729
Hi Andreas,

in C# the ++ operator does not differentiate between the prefix and postfix
form.

Neno Loje
Nov 15 '05 #2
Not quite. C# requires only one overload of ++ and then works out both
the prefix and the post fix for you. e.g.:

struct Counter
{
private int i;

public static Counter operator ++ (Counter obj)
{
obj.i++;

return obj;
}
}

...

Counter c = new Counter (); // c.i == 0

Counter c1 = ++c; // c.i == 1, c1.i == 1

Counter c2 = c++; // c.1 == 2, c2.i == 1

It's more complex with classes because c, c1 and c2 would all be the
same object, unless you returned a new object from ++, but the basic
idea is the same.

Regards,

Jasper Kent

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3
Here's a class version that gives the same results:

class Counter
{
private int i;

public static Counter operator ++ (Counter obj)
{
Counter temp = new Counter ();

temp.i = obj.i + 1;

return temp;
}
}
Jasper Kent

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4
Jasper Kent schrieb:
Here's a class version that gives the same results:

class Counter
{
private int i;

public static Counter operator ++ (Counter obj)
{
Counter temp = new Counter ();

temp.i = obj.i + 1;

return temp;
}
}
Jasper Kent

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Great, that is doing the job!
Thanks a lot,
Andy
Nov 15 '05 #5

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

Similar topics

4
by: Mohammad | last post by:
I'm implementing a c++ template class CScan to minipulate a series of numbers. I have implemented operator() to select a range of numbers it works fine for an expression like: scan1 = scan2(0,...
9
by: Silver | last post by:
Hi everyone, I want to overload the ! operator, so that when I write this in main x! I can get the factorial of x. The problem is, that the operator I want to overload takes no parameter. Most...
6
by: n2xssvv g02gfr12930 | last post by:
Does anyone know of an example of overloading operator ->* Cheers JB
3
by: md | last post by:
Hi, the following code is working for static objects. ie the statement IntArray x(20); my problem is i want to use this overloading operator for dynamically created objects...
2
by: nayannovellus | last post by:
As we know that both copy constructors and overloading = opeartor copies one object to another then whats the difference between copy constructor and overloading = operator. There must be some...
3
by: Aff | last post by:
hi, i wold like to ask, why is it when overloading operator we send int value is there any way around?. e.g. class abc { protected: int *a; private: int operator(int );
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
11
by: dascandy | last post by:
Hello, I was wondering, why is overloading operator. (period) forbidden? It would make a few odd applications possible (dynamic inheritance and transparent remote method invocation spring to my...
0
by: citystud | last post by:
I am trying to convert the c++ code to C#, but find difficulty to convert the overloading operator. Here is the source code. I don't really know how to implement the operator = and operator () in...
1
by: haderika | last post by:
Hey, I'm having trouble overloading the ~ operator (the determinant of the matrix) in a case of 2x2 matrices. So i have a matrix class, with the constructor, overloading +, += and ~ operators. the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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...
0
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...
0
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...
0
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,...

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.