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.
Related
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 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.
I am wondering if there is a way to overload operator[] for a non-class type in C++.
Basically, there is a data type which is a pointer (CFDictionaryRef from CoreFoundation). But it's not a class (i know that overloading operator[] for a specific class is allowed). I know how to access each element inside the CFDictionaryRef (for example, by using CFDictionaryGetIndex(CFIndex index); ). I want to make it simplified so that I don't have to write that function call every time. I want to overload the operator[ ] for CFDictionaryRef. But since it's not a class, from what I see, it's not possible.
Anyone got any suggestions?
You're right, it's not possible to overload operators on non-user-defined types.
What you might do is wrap the pointer in a class and overload the operators on the class itself. Since you can only overload operators on class types, this is the only option.
class CFDictionaryRefWrapper {
public:
CFDictionaryRefWrapper(CFDictionaryRef r) : dref(r) { }
CFDictionaryRef dref;
Type operator[](unsigned int index) {
/* do whatever with dref */
}
};
This also has the advantage of being able to automatically manage the lifetime of the pointer (RAII) if you need to do that.
No, you cannot overload the [] operator for a pointer type, or any other built-in type. In fact, ptr[N] already is a shorthand for *(ptr + N).
You'd need to define your own class which wraps the pointer if you want to overload the [] operator.
So I'm in a basic programming II class. We have to create a program that makes 4 different functions that will change the way an operator works. I've looked up multiple examples and sets of text that display how to do this, but I cannot make which way of what any of the code means. To me something like this should work.
int operator++()
{
variableA--;
}
To me, this says if you encounter a ++, then -- from the variable, now obvious it doesn't work like this. All the examples I've found create their own data type. Is there a way to overload an operator using an int or a double?
All the examples create their own data type since this is one of the rules for operator overloading: An overloaded operator must work on at least one user-defined type.
Even if you could overload ++ for integers, the compiler wouldn't know which one to use -- your version or the regular version; it would be ambiguous.
You seem to think of operators as single functions, but each overload is a completely separate function differentiated by its function signature (type and sometimes number of arguments), while having the same operator symbol (this is the definition of "overloading").
So, you can't overload ++ to always do something different; this would really be operator overriding, which C++ doesn't allow.
You can define ++ for a type you've created though:
class MyType {
public:
int value;
};
MyType const& operator++(MyType& m) { // Prefix
++m.value;
return m;
}
const MyType operator++(MyType& m, int) { // Postfix (the 'int' is just to differentiate it from the prefix version)
MyType temp = m;
++m.value;
return temp;
}
int main() {
MyType m;
m.value = 0;
m++; // Not m.value++
cout << m.value; // Prints 1
}
Note that this set of ++ operators was defined outside of the MyType class, but could have been defined inside instead (they would gain access to non-public members that way), though their implementations would be a little different.
You can't overload operators of built-in types. (Well, technically you can overload things like "int + MyClass" - but not when both sides are built-in types)
Take a look at How can I overload the prefix and postfix forms of operators ++ and --?