I'm trying to use a template with an assignment operator overload.
I want to do it function style, not class template or there is no point.
Also, I think it is trying to use the default assignment operator...
If I move the mouse cursor over a variable, it doesn't do anything, but if I comment out the template line, it recognizes a variable but won't compile unless I remove teh references to the template.
It never runs my overload, I know that because if I have assert(1<0);, it won't crash. If I tell it to cout something, it won't work either.
Here's a simplified version of my code.
Code:
#include <iostream.h>
//#include <string>
class wimpyclass
{
public:
wimpyclass();
~wimpyclass();
void *data;
int getdata();
template <class unknowndatatype>
const wimpyclass & operator = ( const unknowndatatype & rhs ); // assign unknowndatatype
};
wimpyclass::wimpyclass()
{
int x = 0;
data = &x;
}
wimpyclass::~wimpyclass()
{
return;
}
template <class unknowndatatype>
const wimpyclass& wimpyclass::operator =(const unknowndatatype & rhs)
//postcondition: normal assignment via copying has been performed
{
if ( (data != (void*)&rhs) && (this != &rhs) ) // check aliasing
{
data = (void*)&rhs; // assign the new void pointer data
//data = const_cast<void*>(&rhs); // assign the new void pointer data
}
else
cerr << "\n\nCannot assign to itself\n\n";
return *this;
}
int wimpyclass::getdata()
{
return *(int*)&data;
}
int main()
{
wimpyclass junk1;
junk1 = 50;
junk1 = 'a';
cout << junk1.getdata() << endl << *(int*)&junk1.data << endl;
return 0;
}
I have the void *data public so that I can make sure it is working... and it isn't assigning.
However, if I remove the template and replace those with ints, it will work for ints.
So is it possible to get a template to work with an operator overload?