Dear all,
I have an issue with re-implementing the assignment operator of
a base class, of which the derived class is templated.
the code:
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <exception>
using namespace std;
class AbstractProperty {
public:
virtual string getValue() = 0;
virtual void setValue( string value ) = 0;
virtual AbstractProperty & operator = ( AbstractProperty & rhs ) = 0;
};
template< class T >
class Property : public AbstractProperty {
public:
Property( T value ):
m_value( value ){
}
string getValue(){
stringstream ss;
ss << m_value;
return( ss.str() );
}
void setValue( string value ){
stringstream ss;
ss << value;
ss >> m_value;
}
AbstractProperty & operator = ( AbstractProperty & rhs ){
try{
Property & p = dynamic_cast<Property & >( rhs );
this->m_value = p.m_value;
}
catch( exception & e ){
cout << "eek ! a " << e.what( ) << endl;
}
return( * this );
}
private:
T m_value;
};
void main( void ){
int i1 = 1;
int i2 = 2;
float f = 0.5;
Property<int> Pi1( i1 );
Property<int> Pi2( i2 );
Property<float> Pf( f );
Pi2.setValue( "3" );
Pf.setValue( "0.25" );
cout << "Pi1: " << Pi1.getValue() << endl;
cout << "Pi2: " << Pi2.getValue() << endl;
cout << "Pf: " << Pf.getValue() << endl;
Pi1 = Pf; // works, but with a Bad Dynamic (as expected)
Pi1 = Pi2; // doesn't compile, because
// unresolved external symbol "public: virtual class AbstractProperty
// & __thiscall AbstractProperty::operator=(class AbstractProperty &)"
cout << "Pi1: " << Pi1.getValue() << endl;
}
What I *really* don't understand, is why the two Property<int> instances
being assigned results in an unresolved function, shouldnt the two instances
compare as two Property<int> classes, and thus be implemented by the template ?
it's not a matter of the argument to the assignment operator not being constant,
Ive checked.
any input is appreciated,
Jonathan
why doesnt the compiler resolve the assignment operator to be that of the Property<int> class ?
J