This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Operator overloading
Suppose I have a class Foo and I want to to overload the postfix increment operator. I do something like:
class Foo{
.....
public:
friend Foo& operator++(Foo&, int);
and then I define the new operator somewhere. What I really cannot figure out is where does this int come from.
If I have: f = Foo(); I can do:
f++;
and this seems to me to be unary.
How is the syntactic rule?
In the tests I ran the int value passed was 0. Is ti always zero?
int parameter is just a convention, hint for the compiler to differentiate between prefix and postfix operators.
Related
This question already has answers here:
C++ implicit conversions
(4 answers)
Closed 2 years ago.
I was practicing for one of my upcoming exams and I came upon a weird piece of code in an exercise. For context, it's one of those exercises where you have to determine whether the code will run or not, what's wrong with it etc. The code that I find weird is the following:
class A {
int x;
public:
A(int i = 0) { x = i;}
A operator+(const A& a) { return a.x + x;}
...\\ rest of the code is not relevant
};
Naturally, I assumed that the operator would generate an error, because it returns an int value, while its return-type is defined to be class A. However, after plugging the whole example in my IDE, to check whether I was right or not, not only was there no error thrown at the function declaration, but one later addition operation in the code actually "worked"(I'm using quotations marks because the value it returned was garbage, but it didn't throw any errors neither).
Why is this not throwing any errors?
int is implicitly convertible to A, via A(int) constructor. Your operator+ in effect performs return A{a.x + x};
This is not specific to overloaded operators - whenever an expression of one type is provided where another type is expected, the compiler is looking for ways to implicitly convert the former to the latter.
This question already has answers here:
Which operator to overload in order to use my class in an if-statement? [duplicate]
(4 answers)
How do I override the bool operator in a C++ class?
(4 answers)
User Defined Conversions in C++
(4 answers)
Closed 4 years ago.
I am familiar with operator overloading, but a question popped up in my mind.
Is it possible to overload how an object behaves upon evaluating it without any operator? Such as:
Object foo;
if(foo){...}
So i can overload the evaluation with something like:
bool operator evaluation(){
bool isValid=false;
//Some instructions and conditions
return isValid;
}
There's probably little to no reason to do something like this, but I'm a bit new to C++ and I'm just exploring all nooks and crannies of it.
Thank you!
This question already has answers here:
What are the basic rules and idioms for operator overloading?
(8 answers)
Closed 6 years ago.
This function returns a const Matrix A when I plus a Matrix B and C. But then I should not be able to change that const Matrix, for example A = A + D;, but I am. Could someone explain to me why that is possible?
const Matrix operator+(const Matrix&) const;
A + D creates a new Matrix instance without changing A but the problem here is that you're assigning it back to A.
The assignment operator changes A, not your addition operator. The const property cannot extend to the full expression.
This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
Closed 8 years ago.
I saw something like:
const char& operator[] (int Index) const
the first const I understand. It's to protect the returning char from being modified.
But what does second const mean? Why do we sometimes use two const, sometimes just one?
It can be used for any member function, not only operators. It means, that this function will:
not be able to modify any data members (except mutable ones)
will not be able to call any non-const member functions
This question already has answers here:
Throw and ternary operator in C++
(4 answers)
Closed 9 years ago.
Reading the documentation of the ternary operator, I've realized that there are two special cases that I've never used:
you can use it with functions that return void : bool ? void : void
you can throw inside a ternary operator
So is the following valid, completely defined, and oftenly used (assuming that this is a class member, and the class owns a Type _data[Size]) ?
Type& at(const unsigned int i)
{
return (i < Size) ? (_data[i]) : (throw std::out_of_range("ERROR"));
}
Your example is valid and well-defined (assuming suitable definitions of Size and _data). As to "oftenly used" - I personally have never seen such a construct before, for what it's worth.