I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain
overloaded operators don't seem to come with the inheritance. For example:
// TestA.h ---------------------------------------
#include <iostream>
enum Aval
{
FIRST_VALUE,
SECOND_VALUE,
THIRD_VALUE
};
class A
{
private:
Aval value;
public:
A()
{
value = Aval(0);
std::cout << "A() constructor" << std::endl;
}
A( Aval val ) : value(val)
{
std::cout << "A(val) constructor" << std::endl;
}
virtual ~A();
virtual A & operator=( const A & otherError );
virtual A & operator=( const Aval & otherValue );
virtual Aval GetValue() { return value; }
};
// TestA.cpp --------------------------------------
#include "TestA.h"
#include <iostream>
A::~A()
{
std::cout << "A destructed" << std::endl;
}
A & A::operator=( const A & otherError )
{
// Check for self-assignment
if (this == &otherError)
{
return *this;
}
value = otherError.value;
return *this;
}
A & A::operator=( const Aval & otherValue )
{
value = otherValue;
return *this;
}
// Test B.h --------------------------------------
#include "TestA.h"
#include <iostream>
class B : public A
{
public:
B()
{
std::cout << "B() constructor" << std::endl;
}
virtual ~B()
{
std::cout << "B destructed" << std::endl;
}
};
// main.cpp --------------------------------------
int main( int argc, char* argv[] )
{
A testA;
B testB;
testA = FIRST_VALUE;
testB = SECOND_VALUE;
return 0;
}
When compiling this in VC++ 6.0, I get the error for the "testB = SECOND_VALUE" line of code:
error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'enum Aval' (or there is no
acceptable conversion).
The "testA = FIRST_VALUE" seems to be fine. Is this a VC++ 6.0 quirk or am I doing something wrong? Why won't B
inherit the overloaded operator?
Thanks.