This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Operator overloading
I was wonder how can I over load the Conditional operator in cpp?
int a,b,c;
a=10;
b=11;
c = (a>b) ? a : b;
Is it possible?
You cannot overload the conditional operator.
Several operators cannot be overloaded. These operators take a name, rather than an object, as their right operand:
Direct member access (.)
Deference pointer to class member (.*)
Scope resolution (::)
Size of (sizeof)
The conditional operator (?:) also cannot be overloaded.
Additionally, the new typecast operators: static_cast<>, dynamic_cast<>, reinterpret_cast<>, and const_cast<>, and the # and ## preprocessor tokens cannot be overloaded.
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=23
No, you can't overload the conditional operator, since it's simply shorthand for a simple if..else block.
You can however overload the operators used in the condition, but not for primitive types such as int, like you have in your example above.
Related
This question already has answers here:
Overloading member access operators ->, .*
(5 answers)
Closed 3 years ago.
I have implemented a Matrix class implementation for matrix manipulation, and so far i can overload ordinary (single) operators such as +, *, ...
I want to do Matrix elementary multiplication by overloading the operator (.*), such as i can write
Matrix A(5,3), B(5, 3), C;
C = A .* B;
C, in this case, will hold the value of multiplication element by element from A and B (A and B have same dimensions)
From this operator overloading reference
Restrictions
The operators :: (scope resolution), . (member access), .* (member access through pointer to member), and ?: (ternary conditional) cannot be overloaded.
In short, it's not possible to overload the "dot" member access operators like .*.
Here you can see the "pointer to member of object" .* operator is not overloadable in c++.
The postfix operators take an int parameter. There is already a question as to why, and it seems like the answer is: "Because Bjarne Stroustrup said so"
I'm uncomfortable with that answer. If Bjarne Stroustrup needed something to tip the compiler off to behave differently, why couldn't he just key off whether the operator returned a reference? It leaves me questioning:
Why can't I do: foo++ 13;
Why isn't the int parameter defaulted to 1
Why is this considered a unary operator at all, it takes an argument
If Bjarne Stroustrup needed something to tip the compiler off to behave differently, why couldn't he just key off whether the operator returned a reference?
Because you cannot overload a function based on its return type. It is the parameters, the const qualification and reference qualification that the function can be overloaded for
Why can't I do: foo++ 13;
Because the (int) parameter is just there for overload resolution. You do not take it in or use the parameter.
Why isn't the int parameter defaulted to 1
Again it is not used. It is just there to tell the compiler if it is the prefix or postfix version.
Why is this considered a unary operator at all, it takes an argument
It actually doesn't take an argument. The parameter is only there to make them different. It only affects and works on one operand so it is a unary operator.
To answer the question in the title: in foo++, ++ is clearly a unary operator. The fact that its implementation could look like a binary operator if you squint at it just right doesn't change how it's used, and that's what makes it unary.
This question already has answers here:
What does sizeof without () do? [duplicate]
(5 answers)
Closed 8 years ago.
typedef struct rem{
int addr;
char addrbuf[32];
} foo;
Both of these codes return the same results
foo addr;
printf("size is: %d\n",sizeof addr);
printf("size is: %d\n",sizeof (foo));
size is: 36
size is: 36
But when should we use sizeof with and without parentheses?
When using sizeof with a type, you need parentheses around the type. When using it with an expression, you don't. But you can of course include them in this case as well, and you don't have to worry about operator precedence in such case. With uncommon operators such as this one, fewer people would be sure of the precedence, so clarity certainly helps.
So I'd say it's preferable to use them always.
[expr.sizeof]/1:
The operand is either an expression, which is an unevaluated operand
(Clause 5), or a parenthesized type-id.
Thus the parentheses are required for types only. If you prefer to use parentheses for clarity and consistency (as I do) you can always use them though, as parentheses around an expression form another expression.
The operator precedence of sizeof is not very well known and could cause irritations.
Also, for the sizeof... operator, you always have to use parentheses (another reason for consistency).
Can we declare a function like this in c++:
int operator + (int , int);
Your answers will be appreciated!
Thanks
You cannot redefine a built-in operator. Operator overloading
is designed to allow you to extend the language, not to change
it. At least one of the parameters of an overloaded operator
must be a user defined type (class or enum type) or a reference
to a user defined type.
Yes: You can pass ints and floats into overloaded functions
No: You cannot overload/override the operators for built in types when the built-in types are on both sides of the expression.
No we cannot overload integer or float types because overloading means to change the working of existing operators or make them to work with objects int is single member not an object.
While trying to learning operator overloading, I read the following statements from C++ Primer. Frankly speaking, I do not quite understand what does the message that these statements want to deliver. The examples include defining both member binary operator and nonmember binary operator. Is there any difference when using them?
Ordinarily we define the arithmetic and relational operators as nonmember functions and we define assignment operators as members:
Sales_item& Sales_item:: operator (const Sales_item&)
Sales_item operator_(const Sales_item&, const Sales_item&);
Both addition and compound assignment are binary operators, yet these functions define a different number of parameters. The reason for the discrepancy is the this pointer.
Yes, there is a difference in actual use. In particular, when you overload an operator as a non-member function, conversions can be applied to either operand (or both operands). When you overload a binary operator with a member function, conversions can only be applied to the right operand.
This can lead to some oddities. For example, consider writing a "bignum" package and you wanted to overload operator+ to handle bignums. If you overload it as a member function, you get an oddity like this:
int x = 2;
bignum y = 3;
bignum z;
z = y + x; // works fine.
z = x + y; // doesn't work: x isn't a bignum, and can/won't be converted to one
If, instead, you overload operator+ using a non-member function, both of the operations will work (presuming you have a constructor to create a bignum from an int, which you'd almost certainly want).
A few operators (particularly assignment operators, such as =, +=, -=, etc.) are special. A conversion creates a temporary object, and assigning to a temporary object 1) isn't allowed, and 2) wouldn't make sense or accomplish much anyway. Therefore, when you're overloading assignment operators, you always use a member function.