By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,445 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,445 IT Pros & Developers. It's quick & easy.

Overloading Virtual Functions

P: n/a
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?

Dec 18 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Dave wrote:
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<baseAttributeor 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

Dec 18 '06 #2

P: n/a
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()
}

--
WW aka Attila
:::
People don't see the world as it is, but as they are.
Dec 19 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.