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
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