Is it possible to overload the "evaluation operator"? [duplicate] - c++

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!

Related

what does "const" mean when overloading operator in C++ [duplicate]

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

are there any benefits to use initialization instead of assignment in C++? [duplicate]

This question already has answers here:
Is there a difference between copy initialization and direct initialization?
(9 answers)
Direct Initialization vs Copy Initialization for Primitives
(4 answers)
Closed 9 years ago.
I read the following code in c++:
bool result(false);
is result initialized as false?
but are there any benefits to do it instead of:
bool result = false;
can anyone help?

Return throwing ternary operator? [duplicate]

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.

Why is no return type specified in this function clearly returns? [duplicate]

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>.

Overload postfix increment operator [duplicate]

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.