operator-overloading

Homework - Operator-Overloading Currency Class - Stuck/Lost

廉价感情. 提交于 2019-12-23 03:52:07
问题 I need help with creating Operator-Overloaded functions please. I tried 2 but I am stuck. (Thank you all for your help last time! I was able to completely finish :] ). Problem 1: The operator+(const Currency &rhs) will add the 2 dollar amounts, but not the 2 cent amounts, though it keeps the cents from one of them. So, 40.20 + 40.20 = 80.20 (40 Dollars and the 20 cents are entered in separately being "int", wrote it as above for readability display purposes...sorry for the confusion!) //

What advantage is there to overriding the == operator in an ORM?

大城市里の小女人 提交于 2019-12-23 02:03:13
问题 Apparently, a lot of ORMs do something like this: query.filter(username == "bob") to generate SQL like ... WHERE username = 'bob' Why override the == operator instead of something like: query.filter(username.eq("bob")) 回答1: This is a subjective question, but in general I would say that the syntax for the former is more intuitive, and since many (if not most) ORM's do this it's generally expected (making it more intuitive). 回答2: The whole point of orm is brideing the "impedance mismatch"

How to convert C++ class/struct to a primitive/different type/class/struct?

♀尐吖头ヾ 提交于 2019-12-23 00:51:24
问题 I have the following class CppProperty class that holds value: template<typename TT> class CppProperty { TT val; public: CppProperty(void) { } CppProperty(TT aval) : val(aval) { } CppProperty(const CppProperty & rhs) { this->val = rhs.val; } virtual ~CppProperty(void) { } TT operator=(TT aval) { this->val = aval; return this->val; } friend TT operator++(CppProperty & rhs); friend TT operator--(CppProperty & rhs); friend TT operator++(CppProperty & rhs, int); friend TT operator--(CppProperty &

How to convert C++ class/struct to a primitive/different type/class/struct?

别说谁变了你拦得住时间么 提交于 2019-12-23 00:51:08
问题 I have the following class CppProperty class that holds value: template<typename TT> class CppProperty { TT val; public: CppProperty(void) { } CppProperty(TT aval) : val(aval) { } CppProperty(const CppProperty & rhs) { this->val = rhs.val; } virtual ~CppProperty(void) { } TT operator=(TT aval) { this->val = aval; return this->val; } friend TT operator++(CppProperty & rhs); friend TT operator--(CppProperty & rhs); friend TT operator++(CppProperty & rhs, int); friend TT operator--(CppProperty &

Overloaded output operator not found for Boost.Spirit expression

人盡茶涼 提交于 2019-12-22 14:52:39
问题 This is a follow-up on this Q&A. I now have several data structures in a namespace ast , subdivided over two sub-namespaces ( algebraic and numeric ) that correspond to the two different formats that the grammar recognizes. namespace ast { namespace algebraic { struct occupance { char pc; char col; int row; }; using pieces = std::vector<occupance>; struct placement { char c; boost::optional<pieces> p; }; } namespace numeric { struct occupance { char pc; int sq; }; struct range { occupance oc;

CString a = “Hello ” + “World!”; Is it possible?

☆樱花仙子☆ 提交于 2019-12-22 12:37:30
问题 I'm making my own string class and I'd like to ensure that CString a = "Hello " + "World!"; works (i.e. does not give a compiler error such as: cannot add 2 pointers ). My string class automatically converts to char* when needed and thus writing printf(a) would not break the code. Is there any way to replace the compiler behavior around characters ? (i.e. between inverted commas, "abc" ). Or, alternatively, to change the behavior of the + operator to handle strings ? 回答1: The right answer No.

C++ Operator overloading - casting from class

浪子不回头ぞ 提交于 2019-12-22 12:28:24
问题 While porting Windows code to Linux, I encountered the following error message with GCC 4.2.3. (Yes, I'm aware that it's a slight old version, but I can't easily upgrade.) main.cpp:16: error: call of overloaded ‘list(MyClass&)’ is ambiguous /usr/include/c++/4.2/bits/stl_list.h:495: note: candidates are: std::list<_Tp, _Alloc>::list(const std::list<_Tp, _Alloc>&) [with _Tp = unsigned char, _Alloc = std::allocator<unsigned char>] /usr/include/c++/4.2/bits/stl_list.h:484: note: std::list<_Tp,

Can operators be used as functions? (C++)

我是研究僧i 提交于 2019-12-22 11:14:54
问题 This is similar to another question I've asked, but, I've created an expression class that works like so: expression<int, int> exp(10, 11, GreaterThan); //expression<typename T, typename U> exp(T val1, U val2, oper op); //where oper is a pointer to bool function(T, U) where GreaterThan is a previously defined function. And I am wondering why I can't do this: expression<int, int> exp(10, 11, >); particularily when > is overloaded as bool operator>(int a, int a){return (a > b);} which is

Is it possible to overload operator associativity in C++?

99封情书 提交于 2019-12-22 09:37:58
问题 I'm building a class that has a slightly asymmetric addition. Before the complaints come in, it's necessarily asymmetric. There is a conversion (operation that takes a bit of time) that has to happen when two objects are added together, and the conversion most naturally happens with respect to the right summand. To make this concrete, here's a generic example of what's going on... class Foo { char _fav; int _prop; public: const char fav() const {return _fav;} const int prop() const (return

How to properly overload global (+) and (*) without breaking support for other types

匆匆过客 提交于 2019-12-22 09:29:31
问题 I have imported a vector math library, and would like to add my own (*) and (+) operators while preserving the existing operators for basic int and float. I have tried the following: let inline (*) (x : float) (y : Vector) = y.Multiply(x) let inline (*) (x : Vector) (y : float) = x.Multiply(y) let inline (+) (x : Vector) (y : Vector) = x.Add(y) Which has two problems: It seems to remove int + int and int * int , and The 2nd line (which is intended to complete commutativity) does not compile