operator-overloading

In C (also C++), how '&' operator works as both address operator and bitwise operator ? As operator overloading is not supported by C

一个人想着一个人 提交于 2021-01-28 03:28:10
问题 The operator '&' can be used in both of following way int a; scanf("%d",&a); and printf("%d",1&2) . But different behaviour (for first as address operator and second time as bit-wise operator). I know operator overloading is not there in C. Then how it works ?. Also highlight for c++. 回答1: In "C" language, operators have different meaning when they are used as prefix to expression, suffix to expression or "infix" (between two expressions). Consider '*', which performs multiplication as 'infix

Operator overloading/definition for custom classes in Jexl3

浪尽此生 提交于 2021-01-27 20:04:51
问题 I am trying to implement a custom class that should behave like Boolean in Jexl expressions: Example: Object result = jexl.createExpression("a || b").evaluate(context) Where a and b are instances of a custom class that contains an boolean and extra information that should be carried through the evaluated expresssion so that it can be accessed in the end in result . I have read that Jexl3 should support operator overloading and it seems to have all the necessary structures for defining own

Possible to overload curly braces?

左心房为你撑大大i 提交于 2021-01-27 18:30:16
问题 I'm almost sure that not, but I didn't find a difinitive answer to that: Is it possible to overload the curly braces? As in: class Foo { int i; public: int operator{}(int _i){return _i+42;}; }; int main() { Foo f; f{2}; return 0; } It is never mentioned, neither as allowed nor as not allowed. gcc 4.6 doesn't compile it, but that doesn't mean it is not allowed by the standard, right? 回答1: In C++ curly braces {} are not operators unlike the [] (Array subscripting operator) or () (Function call

Can I use const with overloading operators in Rust?

戏子无情 提交于 2021-01-27 17:51:50
问题 In this code: #![allow(dead_code)] use std::ops::Add; struct Foo(i32); const X: i32 = 1; const Y: i32 = X + X; const A: Foo = Foo(1); const B: Foo = A + A; impl Add for Foo { type Output = Foo; fn add(self, rhs: Foo) -> Foo { Foo(self.0 + rhs.0) } } The compiler says: error[E0015]: calls in constants are limited to struct and enum constructors --> src/main.rs:8:16 | 8 | const B: Foo = A + A; | ^^^^^ | note: a limited form of compile-time function evaluation is available on a nightly compiler

Operator overload: Member vs. non-member when only same type objects can be involved

喜欢而已 提交于 2021-01-27 14:08:12
问题 This question gives a good answer why to define operator overloads as non-members: Operator overloading : member function vs. non-member function? If you define your operator overloaded function as member function, then compiler translates expressions like s1 + s2 into s1.operator+(s2). That means, the operator overloaded member function gets invoked on the first operand. That is how member functions work! But what if the first operand is not a class? There's a major problem if we want to

Create C++ integer class to act absolutely identical to integral integer type

牧云@^-^@ 提交于 2021-01-27 03:56:13
问题 Small and pretty nasty problem I've seen several days ago, asked to my friend on interview. The initial interview question was: "What will be the output of the following code?" int i = 2; i = i++ + i++; The correct answer is ((2 + 2) + 1) + 1 = 6, i.e. post-increment is applied twice before assignment, but after addition. Then I wanted to create a simple class carrying one integer and overload operator+() and operator++(int) to see in logs the exact order, in which operators will be executed.

Implementing overloads for >> and << operators in a template class

。_饼干妹妹 提交于 2021-01-21 07:02:30
问题 I'm trying to write the function definition for overloading the operators ">>" and "<<" outside of the class definition, though in the same file, as you can see. I get the following errors: 1>Source.obj : error LNK2019: unresolved external symbol "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class MyClass<int> &)" (??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$MyClass@H@@@Z

why C++ operator overloading requires “having at least one parameter of class type”?

余生颓废 提交于 2021-01-20 19:28:21
问题 The chapter 14.1 of "C++ primer 5th edition" reads, An operator function must either be a member of a class or have at least one parameter of class type. For example, string("hello")+"world" compiles "hello"+"world" doesn't. And when I want to overload the + on two C strings. std::string operator+ (const char* s1, const char* s2) I get the following error. error: ‘std::string operator+(const char*, const char*)’ must have an argument of class or enumerated type I have two questions. Is this

why C++ operator overloading requires “having at least one parameter of class type”?

谁说胖子不能爱 提交于 2021-01-20 19:23:30
问题 The chapter 14.1 of "C++ primer 5th edition" reads, An operator function must either be a member of a class or have at least one parameter of class type. For example, string("hello")+"world" compiles "hello"+"world" doesn't. And when I want to overload the + on two C strings. std::string operator+ (const char* s1, const char* s2) I get the following error. error: ‘std::string operator+(const char*, const char*)’ must have an argument of class or enumerated type I have two questions. Is this

why C++ operator overloading requires “having at least one parameter of class type”?

最后都变了- 提交于 2021-01-20 19:21:36
问题 The chapter 14.1 of "C++ primer 5th edition" reads, An operator function must either be a member of a class or have at least one parameter of class type. For example, string("hello")+"world" compiles "hello"+"world" doesn't. And when I want to overload the + on two C strings. std::string operator+ (const char* s1, const char* s2) I get the following error. error: ‘std::string operator+(const char*, const char*)’ must have an argument of class or enumerated type I have two questions. Is this