473,387 Members | 1,619 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,387 software developers and data experts.

Overridden method doesn't get invoked

Hi,

I've naively written a piece of code that's not doing what I want it to.
I want to store objects in a vector of some base class A. This base
class provides a method doSomething() that is overridden in subclasses.
When I invoke doSomething() I always end up with the implementation
provided by the base class A, regardless the subclass I used for
instantiating the object. I suppose that this is related to the fact
that the vector holds references and not pointers, but that's what it
has to offer. How can I make it work? I've got some example code here
to illustrate my problem.

#include <iostream>
#include <vector>

using namespace std;

class A {
public:
A();
virtual ~A();
virtual double doSomething(int parameter);
};

A::A()
{
}

A::~A()
{
}

double A::doSomething(int parameter)
{
cout << "A: " << parameter << "\n";
return 0.0;
}

class B : public A {
public:
B();
virtual ~B();
virtual double doSomething(int parameter);
};

B::B()
{
}

B::~B()
{
}

double B::doSomething(int parameter)
{
cout << "B: " << parameter << "\n";
return 0.0;
}

class C
{
vector<AItems;
public:
C();
~C();
void addItem(A& item);
A* getItem(int index);
};

C::C() : Items()
{
}

C::~C()
{
}

void C::addItem(A& item)
{
Items.push_back(item);
}

A* C::getItem(int index)
{
return &Items.at(index);
}

int main()
{
C storage;
B* object = new B();
storage.addItem(*object);
A* another = storage.getItem(0);
A* another2 = object;
another->doSomething(123); // doesn't call B::doSomething()
// but that's what I want
another2->doSomething(123); // does call B::doSomething()
delete object;
return 0;
}

Any advice is appreciated.

Kind regards,
Hermann
Oct 27 '06 #1
1 1357
Hermann Lichte wrote:
Hi,

I've naively written a piece of code that's not doing what I want it to.
I want to store objects in a vector of some base class A. This base
class provides a method doSomething() that is overridden in subclasses.
When I invoke doSomething() I always end up with the implementation
provided by the base class A, regardless the subclass I used for
instantiating the object. I suppose that this is related to the fact
that the vector holds references and not pointers, but that's what it
has to offer.
The vector does not hold references, it holds actual objects of type A.

How can I make it work?
You have at least the following options:

a) Use std::vector<A*instead of std::vector<A>. In this case, you need to
take care of memory management on your own.

b) Use std::vector< tr1::shared_ptr<A. In this case, memory management
will be taken care of.

c) Use std::vector< copy_ptr<A or std::vector< clone_ptr<A. These
smart pointers have been discussed in the archives. They provide deep copy
semantics.

d) Use a specialized container for pointers. I understand that several are
available from various libraries.

[snip: code illustrating slicing]
Best

Kai-Uwe Bux
Oct 27 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bryan Ray | last post by:
I am trying to write an inheritance function, so I can call a base classes method that has been overridden in the derived class. I want to get rid of the ugly 'call()' syntax that would be used....
2
by: Bj?rn Toft Madsen | last post by:
Hi all, The network library I use communicates with my app using a callback function. This callback function is called with the type of message, a void pointer to he actual message and a user...
7
by: Gaetan | last post by:
I would like to extend the capabilities of my application by calling a user method residing in a client provided assembly without having to recompile my application. Things would work like this:...
8
by: Allan Ebdrup | last post by:
I'm writing some code where I have have a class that implements 4 methods (class A) I only want to call these methods from the base class if they have been overridden in a sub class (Class B) I...
4
by: ddtl | last post by:
Hello everybody. Consider the following code: class A(object): def met(self): print 'A.met' class B(A): def met(self):
5
by: rn5a | last post by:
The .NET 2.0 documentation states the following: When using a DataSet or DataTable in conjunction with a DataAdapter & a relational data source, use the Delete method of the DataRow to remove...
22
by: jeremito | last post by:
I am writing a class that is intended to be subclassed. What is the proper way to indicate that a sub class must override a method? Thanks, Jeremy
12
by: Ratko | last post by:
Hi all, I was wondering if something like this is possible. Can a base class somehow know if a certain method has been overridden by the subclass? I appreciate any ideas. Thanks, Ratko
6
by: wink | last post by:
I'd like to determine if a method has been overridden as was asked here: http://www.velocityreviews.com/forums/t564224-determining-whether-a-derived-class-overrides-a-virtual-memberfunction.html...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.