conversion-operator

Operator = Overload with Const Variable in C++

Hi everybody. I was wondering if you guys could help me. Here are my .h: Class Doctor { const string name; public: Doctor(); Doctor(string name); Doctor & Doctor::operator=(const Doctor &doc); } and my main: int main(){ Doctor d1 = Doctor("peter"); Doctor d2 = Doctor(); d2 = d1; } I want to ...

Why does virtual assignment behave differently than other virtual functions of the same signature?

While playing with implementing a virtual assignment operator I have ended with a funny behavior. It is not a compiler glitch, since g++ 4.1, 4.3 and VS 2005 share the same behavior. Basically, the virtual operator= behaves differently than any other virtual function with respect to the code that is actually being executed. struct Base...

Swig C++ Lua Pass class by reference

I don't know why I'm having a hard time with this. All I want to do is this: class foo { public: foo(){} ~foo(){} float a,b; }; class foo2 { public: foo2(){} foo2(const foo &f){*this = f;} ~foo2(){} void operator=(const foo& f){ x = f.a; y = f.b; } float x,y; }; /* Usage(...

How do conversion operators work in C++?

Consider this simple example: template <class Type> class smartref { public: smartref() : data(new Type) { } operator Type&(){ return *data; } private: Type* data; }; class person { public: void think() { std::cout << "I am thinking"; } }; int main() { smartref<person> p; p.think(); // why does not the compiler...

Conversion constructor vs. conversion operator: precedence

Reading some questions here on SO about conversion operators and constructors got me thinking about the interaction between them, namely when there is an 'ambiguous' call. Consider the following code: class A; class B { public: B(){} B(const A&) //conversion constructor { cout << "cal...

Common way to call mother-class operator= in C++?

Let's suppose I have a class Dog that inherits from class Animal, you might want to insert a call to Animal::operator= in Dog::operator=. What is the most readable/common way to write it? I think I know those two... static_cast<Animal*>(this)->operator=(other); and this->Animal::operator=(other); ...

explicit copy constructor or implicit parameter by value

I recently read (and unfortunately forgot where), that the best way to write operator= is like this: foo &operator=(foo other) { swap(*this, other); return *this; } instead of this: foo &operator=(const foo &other) { foo copy(other); swap(*this, copy); return *this; } The idea is that if operator= is called with...

C++: Overloading operator=

Okay so I have a class that has 'weak typing' I.E. it can store many different types defined as: #include <string> class myObject{ public: bool isString; std::string strVal; bool isNumber; double numVal; bool isBoolean; bool boolVal; double operator= (const myObject &); }; I would like ...

STL: how to overload operator= for <vector> ?

There's simple example: #include <vector> int main() { vector<int> veci; vector<double> vecd; for(int i = 0;i<10;++i){ veci.push_back(i); vecd.push_back(i); } vecd = veci; // <- THE PROBLEM } The thing I need to know is how to overload operator = so that I could make assignment like this: vector<double> = vector<int>; I'...

Can you catch an exception by the type of a conversion operator?

I don't know how to phrase the question very well in a short subject line, so let me try a longer explanation. Suppose I have these exception classes: class ExceptionTypeA : public std::runtime_error { // stuff }; class ExceptionTypeB : public std::runtime_error { // stuff operator ExceptionTypeA() const; // conversion op...

Const operator overloading problems in C++

I'm having trouble with overloading operator() with a const version: #include <iostream> #include <vector> using namespace std; class Matrix { public: Matrix(int m, int n) { vector<double> tmp(m, 0.0); data.resize(n, tmp); } ~Matrix() { } const double & operator()(int ii, int jj) const { cout ...

Automatically converting an A* into a B*

Hi, Suppose I'm given a class A. I would like to wrap pointers to it into a small class B, some kind of smart pointer, with the constraint that a B* is automatically converted to an A* so that I don't need to rewrite the code that already uses A*. I would therefore want to modify B so that the following compiles... struct A { void...

invalid cast to type 'float'

I'm having problem with my class. I'm going to make comparision operators of my class. Some code: CVariable::operator float () { float rt = 0; std::istringstream Ss (m_value); Ss >> rt; return rt; }; bool CVariable::operator < (const CVariable& other) { if (m_type == STRING || other.Type() == STRING) int i =...

What is an "operator int" function?

What is the "operator int" function below? What does it do? class INT { int a; public: INT(int ix = 0) { a = ix; } /* Starting here: */ operator int() { return a; } /* End */ INT operator ++(int) { return a++; } }; ...