I am trying to implement a template Class with an Operator Overload for = so far it works for non pointer elements. For Pointer Elements it doesn't work exactly as I expect it to, so my question is why this is sow and how do I force c++ do it as I want.
My template Class:
template <class T>
class IBag {
public:
T _val;
void Set(T val) { _val = val; }
T Get() { return _val; }
IBag& operator=(T val) {
this->Set(val);
return *this;
}
operator T() {
return this->Get();
}
};
How it works using the IBag Class:
class IBagExample
{
void showExample() {
IBag<QString*> pbag;
pbag = new QString("Blub"); // This works !
}
};
how it does not compile:
class IBagExample
{
void showExample() {
IBag<QString*> pbag = new QString("Blub"); // This doesn't compile !
}
};
The compiler Error I get is :
error: no viable conversion from 'QString *' to 'IBag<QString *>'
IBag<QString*> pbag2 = new QString("Blub");
^ ~~~~~~~~~~~~~~~~~~~
For me it seems the same, maybe I need to tell the compiler something to understand what type of pointer is now going to be pushed into the pbag. But I have no Idea how to do that.
Using the operator overload like
IBag<QString*> pbag; pbag = new QString("Blub"); // This does compile !
seems just ridiculous.
(Note:The IBag example is just a simplification of the Code I am trying to implement.)
Thanks a lot,