Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

re-implementing virtual assignment operator of a templated class

Avatar of jochemspek
jochemspekFlag for Netherlands asked on
C++Editors IDEs
13 Comments1 Solution459 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrixFlag of United Kingdom of Great Britain and Northern Ireland imageSenior Software Engineer (Avast)

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 13 Comments.
See Answers