Abstract Classes, Templates and inheritance...

P

ptkacz

I've got two classes, B and C, each performing essentially the same
functionality, except for the fact that they each operate on a different
data type, and that they each have a class method that has different logic
to work with the respective datatype. I was thinking that I'd create an
abstract template class A, that would be inhereted by both B and C classes.
All that would be left to do then, is implement a single method in B and C,
saving lots of work (except for the headics). My question is, how in
classes B and C, when each inherits A, how does one specify a type to A?

I might have something like:

template <class T>
class A
{
public:
A();
void commonMethod() {... common code ... };
virtual void notSoCommonMethod() = 0;

private:
T* var;
};


class B : public A
{
public:
B() : A<some type>();

void notSoCommonMethod() { ... code working on <some type> ... return; }
};

class C : public A
{
public:
C() : A<another type>();

void notSoCommonMethod() { ... code working on <another type> ...
return; }
};

I could then just code:

B varB;
C varC;

varB.commonMethod();
varC.commonMethod();

varB.notSoCommonMethod();
varC.notSoCommonMethod();

Have I just answered my own question?

Peter
 
T

Tobias Blomkvist

(e-mail address removed) sade:
saving lots of work (except for the headics). My question is, how in
classes B and C, when each inherits A, how does one specify a type to A?
[snip]


class B : public A
{
public:
B() : A<some type>();

class B : public A<char>
{
public:
B();

Tobias
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top