operator-overloading

Why does the == operator work for Nullable<T> when == is not defined?

戏子无情 提交于 2019-12-17 19:38:22
问题 I was just looking at this answer, which contains the code for Nullable<T> from .NET Reflector, and I noticed two things: An explicit conversion is required when going from Nullable<T> to T . The == operator is not defined. Given these two facts, it surprises me that this compiles: int? value = 10; Assert.IsTrue(value == 10); With the code value == 10 , either value is being magically converted to an int (hence allowing int 's == operator to be used, or the == operator is being magically

simple c++: How to overload the multiplication operator so that float*myClass and myClass*float works

旧时模样 提交于 2019-12-17 18:56:19
问题 class MyClass; int main() { float a = 5; MyClass c1; MyClass c2 = a*c1; MyClass c3 = c1*a; } How can I overload the multiply operator so that both a*c1 and c1*a work? 回答1: Like so: MyClass operator* (float x, const MyClass& y) { //... } MyClass operator* (const MyClass& y, float x) { //... } The second one can also be a member function: class MyClass { //... MyClass operator* (float x); }; The first 2 options work as declarations outside of class scope. 来源: https://stackoverflow.com/questions

How do I override the bool operator in a C++ class?

拈花ヽ惹草 提交于 2019-12-17 18:52:51
问题 I'm defining a ReturnValue class in C++ that needs to report whether a method was successful. I want objects of the class to evaluate to true on success and false on error. Which operator do I override to control the truthiness of my class? 回答1: The simple answer is providing operator bool() const , but you might want to look into the safe bool idiom, where instead of converting to bool (which might in turn be implicitly converted to other integral types) you convert to a different type

Why we need to return reference to istream/ostream while overloading >> and << operators?

别说谁变了你拦得住时间么 提交于 2019-12-17 18:44:42
问题 What happens if I do not return din or dout , actually I'm reading a book in which writer returns back stream references istream & operator>>(istream &din,vector &a) { for(int i=0;i<size;i++) din>>a.v[i]; return din; } ostream & operator<<(ostream &dout,vector &a) { dout<<"("<<a.v[0]; for(int i=1;i<size;i++) dout<<", "<<a.v[i]; dout<<")"; return dout; } 回答1: The reason is a combination of several facts. You want to be able to chain input and output operations as in in >> x >> y; out << z <<

How Do I define a Double Brackets/Double Iterator Operator, Similar to Vector of Vectors'?

拟墨画扇 提交于 2019-12-17 18:09:11
问题 I'm porting code that uses a very large array of floats, which may trigger malloc failures from c to c++. I asked a question about whether I should use vectors or deques and Niki Yoshiuchi generously offered me this example of a safely wrapped type: template<typename T> class VectorDeque { private: enum TYPE { NONE, DEQUE, VECTOR }; std::deque<T> m_d; std::vector<T> m_v; TYPE m_type; ... public: void resize(size_t n) { switch(m_type) { case NONE: try { m_v.resize(n); m_type = VECTOR; } catch

Overloading operator new for a class

一笑奈何 提交于 2019-12-17 18:08:54
问题 When we overload new operator of a class, we declare the function as a member function. For eg: class OpNew { public: OpNew() { cout << "OpNew::OpNew()" << endl;} void* operator new(size_t sz) { cout << "OpNew::new: " << sz << " bytes" << endl; return ::new char[sz]; } }; How does the statement OpNew *obj = new OpNew work under the hood ? as overloaded new is a member of OpNew class not a static. So how does compiler ensure this call to new member function succeeds? 回答1: An operator new() or

Why is overloading operator&() prohibited for classes stored in STL containers?

依然范特西╮ 提交于 2019-12-17 16:36:25
问题 Suddenly in this article ("problem 2") I see a statement that C++ Standard prohibits using STL containers for storing elemants of class if that class has an overloaded operator&() . Having overloaded operator&() can indeed be problematic, but looks like a default "address-of" operator can be used easily through a set of dirty-looking casts that are used in boost::addressof() and are believed to be portable and standard-compilant. Why is having an overloaded operator&() prohibited for classes

How does operator overloading of true and false work?

那年仲夏 提交于 2019-12-17 16:35:48
问题 You can overload operator true and false i looked at examples and found this http://msdn.microsoft.com/en-us/library/aa691312%28v=vs.71%29.aspx I completely dont understand how they work. I know if i write if(obj) and true returns true then the if is executed. It doesnt matter what false returns. However how does false work? in that doc it is suggected that the && operator uses it. I wrote the code below. I dont know how to get && to compile. || gives me a compile error too. How do i get

Inheriting and overriding ostream operator in C++

自作多情 提交于 2019-12-17 16:29:11
问题 I've been trying to find an answer to this, but no one seems to have exactly the same problem as I do. I am working with several derived classes. The ostream operator << for each of these should print out some things common to each, and some things specific to each. Later on, I would like to further derive from these derived classes, and again the new derived classes need to print out some things that are in the "generations" above them. For example: The Base class .h file class Base { int

How to overload array index operator for wrapper class of 2D array?

喜夏-厌秋 提交于 2019-12-17 16:26:32
问题 #define ROW 3 #define COL 4 class Matrix { private: int mat[ROW][COL]; //..... //..... }; int main() { Matrix m; int a = m[0][1]; // reading m[0][2] = m[1][1]; // writing } I think directly it not possible to overload [][] . I think i have to do it indirectly but how to implement it? 回答1: The easier solution is to use the operator() as it allows multiple parameters. class M { public: int& operator()(int x,int y) {return at(x,y);} // .. Stuff to hold data and implement at() }; M a; a(1,2) = 4;