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

abstract methods and inheritence

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

--
<<Remove the del for email>>
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
>
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
Jul 22 '05 #2

P: n/a
Capstar <ne**@deleg.homeip.net> wrote:
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?
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.