Overloading Virtual Functions

D

Dave

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?
 
T

Thomas Tutone

Dave said:
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.

When you say you "create a vector using the base class," what do you
mean exactly? Do you mean you have std::vector<baseAttribute> or a
std::vector<baseAttribute*>? If you mean the former, then you are
experiencing "slicing" - that vector can only only hold baseAttribute
objects, not classes derived from baseAttribute. So if you store a
derived class object in that vector, then the object gets sliced and is
static_cast to a baseAttribute object. Not surprisingly, then, calling
operator== calls the member function for a baseAttribute, not for the
derived class object.

The solution is to store a vector of pointers to baseAttribute, or
(much) better yet, a vector of smart pointers (like tr1::shared_ptr) to
baseAttribute.

Best regards,

Tom
 
W

White Wolf

Dave wrote:
[SNIP]
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?

Nope. You *can* overload them. You just do not want to:

struct A { virtual void foo()=0; };

struct B : A { using A::foo; void foo(int){;} };

struct C : B { void foo(){;} };

int main() {
C c;
c.foo(12); // Hidden by C::foo()
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top