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.
Related
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:
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Operator overloading
I am seeing this in a piece of sample code:
operator Vector2<float>() const {
return Vector2<float>(x, y); }
My 2 questions about this:
1) The function clearly returns, but there's no return type specified?
2) It's not clear exactly what is getting overloaded here, which operator.
It's a conversion operator, and the return type is Vector2<float>.
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.