operator-keyword

What is the best signature for overloaded arithmetic operators in C++?

I had assumed that the canonical form for operator+, assuming the existence of an overloaded operator+= member function, was like this: const T operator+(const T& lhs, const T& rhs) { return T(lhs) +=rhs; } But it was pointed out to me that this would also work: const T operator+ (T lhs, const T& rhs) { return lhs+=rhs; } I...

C++ Operator Ambiguity

Forgive me, for I am fairly new to C++, but I am having some trouble regarding operator ambiguity. I think it is compiler-specific, for the code compiled on my desktop. However, it fails to compile on my laptop. I think I know what's going wrong, but I don't see an elegant way around it. Please let me know if I am making an obvious mista...

C++ compiler unable to find function (namespace related)

I'm working in Visual Studio 2008 on a C++ programming assignment. We were supplied with files that define the following namespace hierarchy (the names are just for the sake of this post, I know "namespace XYZ-NAMESPACE" is redundant): (MAIN-NAMESPACE){ a bunch of functions/classes I need to implement... (EXCEPTIONS-NAMES...

Compiler not creating templated ostream << operator

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) { outpu...

Bitwise AND, Bitwise Inclusive OR question, in Java

I've a few lines of code within a project, that I can't see the value of... buffer[i] = (currentByte & 0x7F) | (currentByte & 0x80); It reads the filebuffer from a file, stored as bytes, and then transfers then to buffer[i] as shown, but I can't understand what the overall purpose is, any ideas? Thanks ...

c++ unable to find operator via implicit conversion?

When writing a class to act as a wrapper around a heap-allocated object, I encountered a problem with implicit type conversion that can be reduced to this simple example. In the code below the wrapper class manages a heap-allocated object and implicitly converts to a reference to that object. This allows the wrapper object to be passed ...

C++ const std::map reference fails to compile

Is there a reason why passing a reference to a STL map as const causes the [] operator to break? I get this compiler error (gcc 4.2) when I use const: error: no match for ‘operator[]’ in ‘map[name]’ Here's the function prototype: void func(const char ch, std::string &str, const std::map<std::string, std::string> &map); And, I ...

How to make an "operator" variable? (C++)

I am working on making an expression class: template<typename T, typename U> class expression { public: expression(T vala, U valb, oper o){val1 = vala; val2 = valb; op = o;} operator bool{return(val1 op val2);} private: T val1; U val2; oper op; }; as you can see, this is somewhat pseudocode, because I need an opera...

Which is preferred: foo(void) or foo() in C++

I have seen two styles of defining conversion operator overload in C++, operator int* (void) const operator int*() const Question 1. I think the two styles (whether add void or not) have the same function, correct? Question 2. Any preference which is better? ...

Reference-type conversion operators: asking for trouble?

When I compile the following code using g++ class A {}; void foo(A&) {} int main() { foo(A()); return 0; } I get the following error messages: > g++ test.cpp -o test test.cpp: In function ‘int main()’: test.cpp:10: error: invalid initialization of non-const reference of type ‘A&’ from a temporary of type ‘A’ test.cpp:6: er...

Overloading += in c++

If I've overloaded operator+ and operator= do I still need to overload operator+= for something like this to work: -?- MyClass mc1, mc2; mc1 += mc2; ...

How do I overload the << operator?

I intend to call a function whenever m_logger<<"hello"<<"world" is called. m_logger is of type ofstream. So i decide to overload << with following signature friend ofstream& operator<<(ofstream &stream,char *str); However the vc compiler gives following error: error C2666: 'operator <<' : 6 overloads have similar conversions Is...

how does ofstream or ostream type cast all types to string?

any system defined user type past to ostream object is converted to a string or char* ? like cout<<4<<"Hello World"; works perfectly fine, how is this achieved? is the << operator overloaded for each and every type? is there a way to achieve it through just one generic overloaded function? what i mean is can i have just one overloaded...

Operator Overloading in C++ as int + obj

Hi Guys, I have following class:- class myclass { size_t st; myclass(size_t pst) { st=pst; } operator int() { return (int)st; } int operator+(int intojb) { return int(st) + intobj; } }; this works fine as long as I use it like this:- char* src="This is test string"...

C++ Operator overloading - casting from class

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(cons...

Table and Column names causing problems

I have an issue when the T4 linq templates generate the classes for my MySql db using subsonic 3. It looks like one of our table names "operator" is causing problems in the Context.cs generated class. In the following line of code in Context.cs Visual Studio sees <operator> as a c# operator and generates a compilation error of "Type exp...

Overloading *(iterator + n) and *(n + iterator) in a C++ iterator class?

(Note: I'm writing this project for learning only; comments about it being redundant are... uh, redundant. ;) I'm trying to implement a random access iterator, but I've found very little literature on the subject, so I'm going by trial and error combined with Wikpedias list of operator overload prototypes. It's worked well enough so far...

Speed difference: separate functor VS operator() inside a big class with *this

I'm using the c++ STL heap algorithms, and I wrote a wrapper class around it so I could do some other stuff. When I tried to use the code below, for example: //! Min-heap wrapper class. class FMMHeap{ public: FMMHeap(Vector &phi) : _phi(phi) {} bool operator()(unsigned p1, unsigned p2) {return fabs(_phi(p1)) > fabs(_phi(p2)); }...

Declaring functors for comparison ??

Hello, I have seen other people questions but found none that applied to what I'm trying to achieve here. I'm trying to sort Entities via my EntityManager class using std::sort and a std::vector<Entity *> /*Entity.h*/ class Entity { public: float x,y; }; struct compareByX{ bool operator()(const GameEntity &a, const GameEntity &b) {...

C++ [] array operator with multiple arguments?

Can I define in C++ an array operator that takes multiple arguments? I tried it like this: const T& operator[](const int i, const int j, const int k) const{ return m_cells[k*m_resSqr+j*m_res+i]; } T& operator[](const int i, const int j, const int k){ return m_cells[k*m_resSqr+j*m_res+i]; } But I'm getting this error: error C28...