operator-overloading

Why must overloaded operators be declared public?

陌路散爱 提交于 2019-12-24 03:27:46
问题 I wanted to overload an operator in a class , and have it private so that it could only be used from within the class. However, when I tried to compile I got the error message "User-defined operator ... must be declared static and public: Why do they have to be public? 回答1: To answer half part of your question you may see the blog post by Eric Lippert. Why are overloaded operators always static in C#? Rather, the question we should be asking ourselves when faced with a potential language

Unresolved external symbol with operator overloading and templates

僤鯓⒐⒋嵵緔 提交于 2019-12-24 03:15:06
问题 In trying to compile this program: namespace MyNamespace { template<typename T> class Test { public: class Inner { int x; public: Inner() : x(0) { } friend Inner& operator++(Inner& rhs); }; Inner i; }; } template<typename T> typename MyNamespace::Test<T>::Inner& operator++(typename MyNamespace::Test<T>::Inner& rhs) { rhs = MyNamespace::Test<T>::Inner(rhs.x + 1); return rhs; } int main() { MyNamespace::Test<int> t; MyNamespace::Test<int>::Inner i = t.i; ++i; } I get the error unresolved

return type of overload operator + exhibit weird behaviour

假如想象 提交于 2019-12-24 03:10:18
问题 I have a class employee #include <iostream> #include <string> using namespace std; class employee { public: double operator + (employee); employee(int); double getSalary(); private: double salary; }; int main() { employee A(400); employee B(800); cout<<A+B; employee C = A+B; cout<<C.getSalary(); } employee::employee(int salary) { this->salary = salary; } double employee::operator + (employee e) { double total; total = e.salary + this->salary; return total; } double employee::getSalary() {

PHP overload “=” operator

梦想的初衷 提交于 2019-12-24 02:41:13
问题 Is there a way to overload the equals operator? Let's say I have this code: $variable1 = "a"; $variable1 = "c"; I would like to save to a log file everytime i assign something to $variable1 without having to do something like: $variable1 = "a"; add_to_some_logfile("a"); $variable1 = "c"; add_to_some_logfile("c"); Is there a way to override the equals operator in order to do some other operation other than just assigning the value to the variable? 回答1: No. PHP doesn't support operator

Using cin >> and cout << to populate fields of a class C++

扶醉桌前 提交于 2019-12-24 01:53:27
问题 I have a class in MyClass.h defined like this: #ifndef MyClass_h #define MyClass_h #include <iostream> #include <stdio.h> #include <string> using namespace std; class MyClass { public: string input void ReadFrom(istream &is); void WriteTo(ostream &os) const; }; #endif /* MyClass_h */ MyClass.cpp looks like this: #include <stdio.h> #include <string> #include <iostream> #include "MyClass.h" using namespace std; void MyClass::ReadFrom(istream &is) { // put data into member 'input' } void MyClass

Overloaded assignment operator causes warning about recursion

試著忘記壹切 提交于 2019-12-24 01:10:55
问题 I need to implement the overloaded the assignment operator in a class so the vector.erase function will work properly as proposed in the answers to "vector::erase with pointer member". I have implemented also a copy constructor for the same purpose. By the following implementation of the operator I get the warning : 'Player::operator=' : recursive on all control paths, function will cause runtime stack overflow. Apparently the implementation of Player::operator= is incorrect. What is the

Create templates overloading friend operators in template class

点点圈 提交于 2019-12-24 00:36:26
问题 I really don't know how to create this properly. I have templates class with overloads for operators. And as i know i need to create templates to overload this operators for more universal form. My problem is that code not compile and i don't know how to fix it Example: NSize<30> a(101); NSize<25> b(120); NSize<30> c(115); NSize<30> res = (a*b)*(a*c)*(c*b)*(b*a)*(a*c); In the end, I should be able to do it. For now i defined it as : template <int length> friend NSize operator * (const NSize

What does & = in C& operator=(const C&) & = default; do? [duplicate]

随声附和 提交于 2019-12-24 00:07:22
问题 This question already has answers here : What is “rvalue reference for *this”? (3 answers) Closed 5 years ago . A few questions on SO use a particular syntax for declaring default assignment operators. Rule-of-Three becomes Rule-of-Five with C++11? class C { C(const C&) = default; C(C&&) = default; C& operator=(const C&) & = default; C& operator=(C&&) & = default; virtual ~C() { } }; I'm confused by the & = used for the assignment operators. After a quick test, default assignment operator

Implicit conversion of collections

扶醉桌前 提交于 2019-12-23 22:45:04
问题 If I define an explicit conversion operator between two types, shouldn't it follow that I can explicitly convert between collections of those types? Ie. public static explicit operator FooEntity(Entity entity) { FooEntity e = new FooEntity(entity); return e; } And thus I could do this, IEnumerable<Entity> entities = GetEntities(); IEnumerable<FooEntity> fooEntities = (IEnumerable<FooEntity>)entities; or IEnumerable<FooEntity> fooEntities = entities as IEnumerable<FooEntity> Is this possible

Compiler not creating templated ostream << operator

心已入冬 提交于 2019-12-23 21:04:03
问题 I have a class, defined in a head as: template <typename T> class MyClass { template <typename U> friend std::ostream& operator<<(std::ostream& output, const MyClass<U>& p); public: ... } In an implementation file, I have: template <typename U> std::ostream& operator<<(std::ostream& output, const MyClass<U>& m) { output << "Some stuff"; return output; } Which all looks fairly kosher. However, when I try and use this operator (i.e. std::cout << MyClass()), I get the following linker error: