Hi,
I have a problem I'm trying to solve, and can't seem to find the right
solution.
The Background:
Orginally I had a class which had three private member variables -
name, type and value. These were all strings. I created several
instances of these classes and stored them in a vector.
As part of the program the objects are compared with one another so I
added the bool operator== method to my class.
The problem:
The type of data used to create my classes has changed. It used to be
all strings, but now the member variable "value" could be an int or
bool.
I thought I could create a base class and then inherit from it. Then
create the vector using the base class, but add instances of my dervied
types and still compare them. But I can seem to successfully overload
the operator==. Is it possible to do this??
Here is an example of my code.
class baseAttribute
{
public:
baseAttribute(){};
~baseAttribute(){};
virtual bool operator==(const baseAttribute &) const {return
true};
}
class baseStringAttribute : public baseAttribute
{
public:
baseStringAttribute(){};
~baseStringAttribute(){};
bool operator==(const baseAttribute &) const {return true};
private:
string name;
string type;
string value;
}
class baseStringAttribute : public baseAttribute
{
public:
baseStringAttribute(){};
~baseStringAttribute(){};
bool operator==(const baseAttribute &) const;
private:
string name;
string type;
int value;
}
I do some standard comparison in the operator== function, but every
time I compare objects it seems to call the base classes operator==.
Any ideas why this is? I then tried adding a pure virtual function to
base class, which would be used to compare two objects, but the
complier complains when I try to override it.
When I looked into it further it seems that I can override pure virtual
functions, but I can't actually overload them - is this correct?