operator-overloading

overloading operator >>()

My knowledge of C++ is small as I have only taken a couple classes. I undersand the basic case of using a friend function to overload the input, output stream operator for the "simple book example" of a Point object that has x, y instance variables. I am looking at a real project now, trying to understand what someone wrote and am gett...

Where can one find a list of operator overloads?

Where can one find a list of the function signatures for all operator overloads? ...

overloading the = operator with inheritance

hey i am trying to understand how to overload the operator= when there is an inheritance with no Success. code example: class Person { private: char* m_name; char* m_lastName; ..... public: virtual Person& operator=(const Person& other); }; /********************/ cpp implementation /****...

How to overload operator==() for a pointer to the class?

I have a class called AString. It is pretty basic: class AString { public: AString(const char *pSetString = NULL); ~AString(); bool operator==(const AString &pSetString); ... protected: char *pData; int iDataSize; } Now I want to write code like this: AString *myString = new AString("foo"); if (myString == ...

c++ fraction class. overloading operators?

I am making a fraction class for a school project, and my brain is frying. I was told to overload the << and >> operators through the friend keyword. But I'm getting errors for this. I've posted the relevant code here: http://pastebin.com/NgCABGJ2 The errors include: error C2270: '<<' : modifiers not allowed on nonmember functions (thi...

A problem overloading [] in 2 variations

Hi this is my generic class: template<class T, class PrnT> class PersonalVec { public: PersonalVec(); T &operator[](int index) const; const T &operator[](int index) const; private: std::vector<T> _vec; }; I'm required to implement 2 versions of [] operator: one that will return a const reference and a regular one t...

Unary Operator-() on zero values - c++

I wrote this code to overload the unary operator- on a matrix class: const RegMatrix RegMatrix::operator-()const{ RegMatrix result(numRow,numCol); int i,j; for(i=0;i<numRow;++i) for(j=0;j<numCol;++j){ result.setElement(i,j,(-_matrix[i][j])); } return result; } When i ran my program with...

Expression template operator overloading problem with std::vector.

I'm currently working on a numerical library that uses expression templates. Unfortunately I encountered a problem with my operator overloads. Consider the following stripped down example. #include <vector> namespace test { class test {}; template<class A, class B> class testExpr {}; template<class A, class B> tes...

Defining operator< for a struct

I sometimes use small structs as keys in maps, and so I have to define an operator< for them. Usually, this ends up looking something like this: struct MyStruct { int a; int b; int c; bool operator<(const MyStruct& rhs) const { if (a < rhs.a) { return true; } else if (a ==...

Overloading increment operator, looping, and edge cases.

Hello! I have an enum, that looks like this: enum Suit {Clubs, Diamonds, Hearts, Spades}; I want to overload the increment operator, so I can easily loop over these four dudes. When the variable is Clubs, Diamonds, or Hearts there no issue. Its the Spades condition that is giving me a little trouble. My first instinct was to define...

template class + operators + friends = unresolved externals

I have a class called fraction, and I'm declaring some operators as friends. I declared the friend operators beforehand, as http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16 told me to do, but it only fixed +, -, *, and /. << and >> still don't work. template <class T> class Fraction; template <class T> Fraction<T> operator...

operator overload

I am working on this project just trying to keep up my c++ knowledge. Anyways, I am getting many, many errors when i try to implement an operator overload. Not sure why. #include "students.h" #include <iostream> #include "Quack.h" using namespace std; void main() { quack* classmates = new quack; classmates->pushFront(students("core...

Operator overloading and namespaces

Possible Duplicate: Where should non-member operator overloads be placed? While browsing on SO, I often find questions or answer that involves overloading/defining a std::ostream& operator<<(std::ostream& os, const Foo& foo) or a Foo operator+(const Foo& l, const Foo& r). While I know how and when (not) to write these operato...

Overload binary +

Hello, I have abstract class: #include <string> using namespace std; class AString { public: virtual ~AString() {} virtual const string& GetName() const = 0; virtual const string& GetValue() const = 0; virtual const int GetSize() const = 0; }; And derivative class. I try to overload binary + in ...

Operator overloading in c++

struct T { int a; int b; }; class Ptr { public: Ptr(int a, int b) { t_.a = a; t_.b = b; } T* operator->() {return &t_;} T& operator*() {return t_;} private: T t_; }; int main() { Ptr ptr(1, 2); cout << "a = " << ptr->a << " b = " << ptr->b << endl; cout << "a = " ...

operator[]= overload?

Okay, I'm trying to make a quick little class to work as a sort of hash table. If I can get it to work then I should be able to do this: StringHash* hash = new StringHash; hash["test"] = "This is a test"; printf(hash["test"]); And it should print out "This is a test". It looks like I have 2 problems at this point. Firstly I d...

class implementation of operator delete() not being invoked

I have the following code #include <iostream> #include <cstddef> #include <string> #include <memory> class Object { public: Object() { std::cout << __PRETTY_FUNCTION__ << std::endl; } std::string x; void *operator new( size_t bytes ) { std::cout << __PRETTY_FUNC...

Is it possible to overload from/import in Python?

Is it possible to overload the from/import statement in Python? For example, assuming jvm_object is an instance of class JVM, is it possible to write this code: class JVM(object): def import_func(self, cls): return something... jvm = JVM() # would invoke JVM.import_func from jvm import Foo ...

Rationale of enforcing some operators to be members

There are 4 operators in C++ which can be overloaded but cannot be overloaded as freestanding (aka nonmember, standalone) functions. These operators are: operator = operator () operator -> operator [] This thread explains perfectly well the rationale of prohibiting operator = to be a member function. Any ideas about the 3 others? TI...

Overloading in C++

I am not sure if I am doing the overload correctly. ...\point.h(42) : error C2061: syntax error : identifier 'Vec3' Point operator +(Vec3 a) const; Here is my .h file: #include <fstream> #include <iostream> #include "vec3.h" using std::ofstream; using std::ifstream; using std::cout; using std::cin; usi...