abstract methods and inheritence

C

Capstar

Hi NG,

I have the following code:

/* example.h */
class foo_base
{
public:
virtual ~foo_base();

protected:
foo_base();
};

class bar_base
{
public:
virtual ~bar_base();
virtual void do_something(foo_base *object) = 0;

protected:
bar_base();
};

class foo : public foo_base
{
public:
foo();
~foo();
};

class bar : public bar_base
{
public:
bar();
~bar();
void do_something(foo_base *object);
};

/* example.cpp */
#include <iostream>
using namespace std;

#include "example.h"

foo_base::foo_base()
{
cout << "constructing foo_base object" << endl;
}

foo_base::~foo_base()
{
cout << "destructing foo_base object" << endl;
}

bar_base::bar_base()
{
cout << "constructing bar_base object" << endl;
}

bar_base::~bar_base()
{
cout << "destructing bar_base object" << endl;
}

foo::foo()
{
cout << "constructing foo object" << endl;
}

foo::~foo()
{
cout << "destructing foo object" << endl;
}

bar::bar()
{
cout << "constructing bar object" << endl;
}

bar::~bar()
{
cout << "destructing bar object" << endl;
}

void bar::do_something(foo_base *object)
{
cout << "doing something with a foo_base object," << endl;
cout << "but I would like to do something with a foo object" << endl;
}

int main(void)
{
foo F;
bar B;

B.do_something(&F);

return 0;
}

But what I realy want is to have
void bar::do_something(foo *object). I can offcourse just make this
method, but that would mean I should remove bar_base::do_something(foo
*object). But I don't want that, because I want every derived class of
bar_base to have that method with it's own derived class of foo_base.
Is this possible someway or am I going the wrong direction here?

Thanks in advance,
Mark
 
J

John Harrison

But what I realy want is to have
void bar::do_something(foo *object). I can offcourse just make this
method, but that would mean I should remove bar_base::do_something(foo
*object). But I don't want that, because I want every derived class of
bar_base to have that method with it's own derived class of foo_base.
Is this possible someway or am I going the wrong direction here?

Thanks in advance,
Mark

It is possible but it is messy. How messy depends on how many different
classes you have, and if you have a situation where you want to add new
classes in the future then its a significant maintenance headache.

The technique is called double dispatch (or multiple dispatch for the really
masochistic). Try googling or get hold of More Effective C++ by Scott
Meyers.

John
 
D

Daniel T.

Capstar said:
Hi NG,

I have the following code: [snip]
But what I realy want is to have
void bar::do_something(foo *object). I can offcourse just make this
method, but that would mean I should remove bar_base::do_something(foo
*object). But I don't want that, because I want every derived class of
bar_base to have that method with it's own derived class of foo_base.
Is this possible someway or am I going the wrong direction here?

The code you wrote implies that any dirivitive of bar_base can handle
any dirivitive of foo_base. The text you wrote imples that each
dirivitive of bar_base can only handle one particular dirivitive of
foo_base. Which is it?
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top