473,326 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Overloading Virtual Functions

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
2 1856
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Xiangliang Meng | last post by:
Hi, all. When reading C++ books, I'm alway confused by those terms "redefining functions", "overloading functions" and "overriding functions". Please give me some comments on those terms....
11
by: iceColdFire | last post by:
Hi, What is the Diff btwn Function overloading and overriding thanks, a.a.cpp
3
by: Joe P | last post by:
Hi, just wondering if there is something in the C++ language standard that forbids the following construct: class Base { public: virtual int GetSomething( int x, int y ); int GetSomething(...
15
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
1
by: ravinderthakur | last post by:
hi all experts, i was just thinking about making overloaded operators virtual. i was wordering what could be the implications of such scenarios. i will be thankful if somebody can provide...
5
by: toton | last post by:
Hi, I want a few of my class to overload from a base class, where the base class contains common functionality. This is to avoid repetition of code, and may be reducing amount of code in binary,...
3
by: Chameleon | last post by:
What is better if you want upcasting in intermediate classes like below? Multiple Inheritance and Overloading or simply RTTI? RTTI wants time but MI and Overloading create big objects (because of...
8
by: yashwant pinge | last post by:
#include<iostream> using namespace std; class base { public: void display() { } };
5
by: tomas | last post by:
Hi. I wrote a code similar to this one, for a wrapper application that I needed: #include <iostream> using namespace std; class Client {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.