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.
Related
This question already has answers here:
How to evaluate an object directly in a boolean context?
(2 answers)
Closed 10 months ago.
I have a Line that represents the relevant information of a line on the cartesian plane. The type that has, among other members, a bool that indicates whether the slope is defined. I would like to be able to do the following:
if(my_line){
double new_slope = my_line.slope * 9;
}
where the instance my_line itself is implicitly converted to a true/false value in a boolean context. I am thinking of the behavior I see with smart pointers, where if it is pointing to nullptr or 0, the instance is considered false as-is.
How would I go about emulating this behavior?
In your Line class, implement a bool conversion operator. You could also optionally overload the operator!, but that is not required in C++11 and later. See Contextual conversions.
For example:
class Line {
bool mSlopeDefined;
...
public:
...
explicit operator bool() const noexcept {
return mSlopeDefined;
}
// optional since C++11, but doesn't hurt...
bool operator!() const {
return !mSlopeDefined;
}
};
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:
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.