I was looking at an iterator class for a linked list and saw this operator overloading and didn't really understand what was going on. I was under the impression that 'int' always has to be declared with a variable name?
void operator++(int)
{
assert(m_node != NULL);
m_node = m_node->m_next;
}
void operator--(int)
{
assert(m_node != NULL);
m_node = m_node->m_prev;
}
Parameter names are always optional. However, the int in this case is special as it denotes this is a postfix operator meaning you are able to do list++ and list--.
Reference: http://en.cppreference.com/w/cpp/language/operators
You can always leave out a parameter name if you want to. If you do that in a normal function definition, that means that an argument must still be provided to the function, but the function doesn't use it:
void f(int) // unnamed parameter
{
// can't use the parameter without a name
}
f(); // ERROR: wants an int
f(42); // OK: takes an int (but ignores it)
In the case of the increment and decrement operators, an unused int parameter is the magic that indicates that this is the postfix operator, x++, not the prefix operator, ++x, which has no parameters.
Because you have two ++ operators, it is a special syntax to differentiate them between post- and pre- incrementation.
void operator++(int)
means postincrementation
void operator++()
means preincrementation
So in your case, you first return, then increase.
void operator++(int)
This means post increment operator. int is just a dummy variable to distinguish it from pre-increment operator. Compiler would pass 0 in its place when calling post increment operator.
Related
defining function->
car operator *()
{
car temp;
temp.a = a*3;
temp.b = b*3;
return temp;
}
in given below code,how to decide whether * is before o2 or after o2.
car o3=*o2 //o2 is also an object of same class and has some values for a and b
There is only one unary operator that can appear on either side of an object. It is disambiguated by providing two overloads: one that takes only one argument and one that takes an int as its second argument. The compiler knows about that, and calls it appropriately:
struct S {
S& operator++(); // pre-increment
S operator++(int); // post-increment
};
S s;
++s; // calls S::operator++()
s++; // calls S::operator++(int)
Yes, that extra argument is a hack. No, you can't do anything meaningful with its value.
All the other unary operators that can be overloaded appear in the same place as the non-overloaded version. If the operator goes in front of the object in a non-overloaded context (e.g., *ptr), then it goes in front of the object in an overloaded context (e.g., *my_object). If it comes after, it comes after.
When overloading the postfix operator, I can do something simple like
Class Foo
{
private:
int someBS;
public:
//declaration of pre &postfix++
Foo operator++();
//rest of class not shown
};
Prefix doesn't need to take any parameters, so when I define it, something like
Foo Foo::operator()
{
someBS ++;
return *this;
}
and it makes perfect sense to me.
When I go to define the postfix overload I have to include a dummy int parameter
Foo Foo::operator++(int)
{
Foo temp = *this;
someBS ++;
return temp;
}
My question is why? I don't ever use it in the method. The prefix operator doesn't require one. The postfix returning the temp value is not dependent on the dummy parameter. I know that if I want to overload a postfix operator that's how it's done, I just want to know the reason behind.
The dummy parameter is simply there to distinguish between the postfix and prefix operators. The name ++ or -- is the same in both cases, so there has to be some way to specify which one you're defining. Adding a dummy parameter is perhaps not elegant, but any alternatives would probably have required inventing new syntax (perhaps a postfix keyword, which would break code that uses postfix as an identifier).
Citing C++reference here:
Prefix versions of the built-in operators return references and postfix versions return values, and typical user-defined overloads follow the pattern so that the user-defined operators can be used in the same manner as the built-ins.
Explaining this logically:
int a = 3;
int b = 0;
int c = a + ++b;
int d = a++ + b;
In the third line, ++b gives you a reference to the updated value; in the fourth line, that's not possible: a has to be increased, so a value-copy is made, and added to b. That dictates different semantics for the operators.
In fact, just to avoid having another operator name:
The int parameter is a dummy parameter used to differentiate between prefix and postfix versions of the operators. When the user-defined postfix operator is called, the value passed in that parameter is always zero, although it may be changed by calling the operator using function call notation (e.g., a.operator++(2) or operator++(a, 2)).
To quote cppreference:
The int parameter is a dummy parameter used to differentiate between prefix and postfix versions of the operators.
The quote says it all, because how else would you differentiate prefix and postfix?
Let's just say that you don't need the int parameter, then the prefix (Foo operator++()) and postfix (Foo operator++()) would be exactly the same! How would the compiler know which one you meant? That's why there is this dummy int parameter.
When overloading the postfix operator, I can do something simple like
Class Foo
{
private:
int someBS;
public:
//declaration of pre &postfix++
Foo operator++();
//rest of class not shown
};
Prefix doesn't need to take any parameters, so when I define it, something like
Foo Foo::operator()
{
someBS ++;
return *this;
}
and it makes perfect sense to me.
When I go to define the postfix overload I have to include a dummy int parameter
Foo Foo::operator++(int)
{
Foo temp = *this;
someBS ++;
return temp;
}
My question is why? I don't ever use it in the method. The prefix operator doesn't require one. The postfix returning the temp value is not dependent on the dummy parameter. I know that if I want to overload a postfix operator that's how it's done, I just want to know the reason behind.
The dummy parameter is simply there to distinguish between the postfix and prefix operators. The name ++ or -- is the same in both cases, so there has to be some way to specify which one you're defining. Adding a dummy parameter is perhaps not elegant, but any alternatives would probably have required inventing new syntax (perhaps a postfix keyword, which would break code that uses postfix as an identifier).
Citing C++reference here:
Prefix versions of the built-in operators return references and postfix versions return values, and typical user-defined overloads follow the pattern so that the user-defined operators can be used in the same manner as the built-ins.
Explaining this logically:
int a = 3;
int b = 0;
int c = a + ++b;
int d = a++ + b;
In the third line, ++b gives you a reference to the updated value; in the fourth line, that's not possible: a has to be increased, so a value-copy is made, and added to b. That dictates different semantics for the operators.
In fact, just to avoid having another operator name:
The int parameter is a dummy parameter used to differentiate between prefix and postfix versions of the operators. When the user-defined postfix operator is called, the value passed in that parameter is always zero, although it may be changed by calling the operator using function call notation (e.g., a.operator++(2) or operator++(a, 2)).
To quote cppreference:
The int parameter is a dummy parameter used to differentiate between prefix and postfix versions of the operators.
The quote says it all, because how else would you differentiate prefix and postfix?
Let's just say that you don't need the int parameter, then the prefix (Foo operator++()) and postfix (Foo operator++()) would be exactly the same! How would the compiler know which one you meant? That's why there is this dummy int parameter.
I tried find anything about overload operator ++ for pointer, but without results.
There is my code.
struct Item
{
int amount;
Item(int a){ this->amount = a; };
Item& operator++()
{
this->amount++;
return *this;
};
};
int main()
{
Item *I = new Item(5);
++*I;
return 0;
}
Is there any options that a could write only in main function
++I;
(sorry about my English)
Just don't use a pointer (there's no need for it anyway):
Item I(5);
++I;
You can manually write (if you REALLY want to use a pointer)
I->operator++();
No, you cannot overload operators for pointer types.
When defining your own operator++ the first argument to the function must be of class- or enumeration-type, as specified in [over.inc]p1 in the C++ Standard:
The user-defined function called operator++ implements the prefix and postfix ++ operator. If this function is a member function with no parameters, or a non-member function with one parameter of class or enumeration type, it defines the prefix increment operator ++ for objects of that type.
But I really really want to call operator++ without having to explicitly write *ptr...
And you can, but it's not the prettiest looking line of code:
ptr->operator++ (); // call member function `operator++`
Note: Instead of doing Item *I = new Item (5); ++*I, consider using just Item I (5); ++I, there's no need to use pointers in your snippet.
As I know here is the way to overload the post-increment operator:
const MyClass& MyClass::operator++(int);
Why does it have int as an argument?
D&E, §11.5.3:
I conisdered the obvious solution, adding the keywords prefix and postfix to C++ [ ... ]
However I received the usual howl of outrage from people who dislike new keywords. Several alternatives that did not involve new keywords were suggested. For example:
class Ptr_to_X {
X ++operator(); // prefix ++
X operator++(); // postfix ++
};
or
class Ptr_to_X {
X& operator++(); // postfix because it
// returns a reference
x operator++(); // prefix because it
// doesn't return a reference
};
I considered the former too cute and the latter too subtle. Finally I settled on:
class Ptr_to_X {
X operator++(); // prefix: no argument
X operator++(int); // postfix: because of the argument
};
This may be too cute and too subtle, but it works, requires no new syntax, and has a logic to the madness. Other unary operators are prefix and take no arguments when defined as member functions. The "odd" and unused dummy int argument is used to indicate the odd postfix operators. In other words, in the postfix case, ++ comes between the first (real) operand and the second (dummy) argument and is thus postfix.
These explanations are needed because the mechanism is unique, and therefore a bit of a wart. Given a choice I would probably have introduced the prefix and postfix keywords, but that didn't appear feasible at the time.
This is to distinguish between prefix increment and postfix increment operators.
For completeness, this is set out in §13.5.7 in both the C++03 and the C++11 standards.