operator-overloading

Correct use of `= delete` for methods in classes

﹥>﹥吖頭↗ 提交于 2019-12-20 08:48:55
问题 Is the following snipplet correct for un-defining all otherwise generated methods and constructors for a class? struct Picture { // 'explicit': no accidental cast from string to Picture explicit Picture(const string &filename) { /* load image from file */ } // no accidental construction, i.e. temporaries and the like Picture() = delete; // no copy Picture(const Picture&) = delete; // no assign Picture& operator=(const Picture&) = delete; // no move Picture(Picture&&) = delete; // no move

Derive overloaded operator, but operate on same types only

旧时模样 提交于 2019-12-20 06:28:19
问题 Suppose I have a base class and two classes derived from it: class Base { protected: double value; public: virtual ~Base(); Base(double value) : value(value) {} Base(const Base& B) { value=B.value; } Base operator+ (const Base& B) const { return Base(value+B.value); } }; class final Derived1 : public Base { public: Derived1(double value) : Base(value) {} }; class final Derived2 : public Base { public: Derived2(double value) : Base(value) {} }; I want to accomplish the following: int main(int

assignment operator overloading skipped / not happening

夙愿已清 提交于 2019-12-20 04:19:55
问题 I'm trying to create a library for some work and am using operator overloading for assignment operation. Supposed X and Y are two instances of the class that has = overloaded thus: A& A::operator=(A &rhs) { A::assign(*this, rhs); return *this; } When I do this: A z; z = x + y; // x and y are other instances of class A everything is fine, however, when I do a ` A p = q + r; the overloaded routine does not get called. I'm not very experienced with operator overloading, can somebody explain

How to overload cout behaviour in c++

霸气de小男生 提交于 2019-12-20 03:11:53
问题 I would like to make cout will always print additional string(above and under) whenever I call it. It is actually weird to me cause I use in Java and C# primaly . EDIT: Maybe we can define other value for "y" string or something like that? Example code: #include <iostream> int main(){std::cout<<"y\n";} result(printed): x y z I would not want to change int main() method just maybe overriding the << for the string type? OR MAYBE make cout invoke additional method? 回答1: I don't think you can do

+= on a vector without boost

Deadly 提交于 2019-12-20 02:57:08
问题 Is there any way to use the += operator with a vector without using boost or using a derivated class? Eg. somevector += 1, 2, 3, 4, 5, 6, 7; would actually be somevector.push_back(1); somevector.push_back(2); somevector.push_back(3); etc. 回答1: With a little ugly operator overloading, this isn't too difficult to accomplish. This solution could easily be made more generic, but it should serve as an adequate example. #include <vector> Your desired syntax uses two operators: the += operator and

C++: string operator overload

时光总嘲笑我的痴心妄想 提交于 2019-12-20 02:55:09
问题 Can I overload existing function/operator in existing class? I was trying to do: #include <iostream> #include <string> using namespace std; string& string::operator<<(const string& str) { this->append(str); } But this gives me error: test.cpp:5: error: too few template-parameter-lists How can I do this? Or I can't? 回答1: You can't add member functions to a class unless you modify that class' definition. Use a free function instead: string& operator<<(string & lhs, const string & rhs) { return

How do I make a class assignable to primitives? Or, how do I make a scalar class?

天涯浪子 提交于 2019-12-20 02:11:10
问题 I was wondering if it's possible to make my class Time { public: Time(); explicit Time( const double& d); Time& operator=( const Time& time); Time& operator=( const double& d); }; assignable to the primitive double? I'm using Time as an IV a lot and need to do a lot of scalar operations on it, so it needs to "mingle" with DV's which are usually ordinary doubles. Adding a second assignment operator did the trick the other way around. A lot of operations still aren't possible with just this

How does non idiomatic global operator overloading work?

可紊 提交于 2019-12-20 02:11:00
问题 I want to understand the code from this answer type Mult = Mult with static member inline ($) (Mult, v1: 'a list) = fun (v2: 'b list) -> v1 |> List.collect (fun x -> v2 |> List.map (fun y -> (x, y))) : list<'a * 'b> static member inline ($) (Mult, v1:'a ) = fun (v2:'a) -> v1 * v2 :'a let inline (*) v1 v2 = (Mult $ v1) v2 F# can resolve overloaded members. (Because it doesn't support currying of members). So, I supposed, it should work for methods as well But it doesn't: type Mult = Mult with

How does non idiomatic global operator overloading work?

半世苍凉 提交于 2019-12-20 02:09:21
问题 I want to understand the code from this answer type Mult = Mult with static member inline ($) (Mult, v1: 'a list) = fun (v2: 'b list) -> v1 |> List.collect (fun x -> v2 |> List.map (fun y -> (x, y))) : list<'a * 'b> static member inline ($) (Mult, v1:'a ) = fun (v2:'a) -> v1 * v2 :'a let inline (*) v1 v2 = (Mult $ v1) v2 F# can resolve overloaded members. (Because it doesn't support currying of members). So, I supposed, it should work for methods as well But it doesn't: type Mult = Mult with

Overloading operator-> in C++

﹥>﹥吖頭↗ 提交于 2019-12-20 01:14:18
问题 I have a smart pointer class and I want to overload operator-> ; it's provided for convenience so I can access the members of the class contained inside the smart pointer directly. I was looking at the way Boost implements this operator in its shared_ptr template. I noticed they added an assert checking if the pointer is indeed non-null before returning it. Currently, my operator returns the pointer without checking if it's null (essentially, a null pointer is undefined behavior in my current