Reputation: 369
I have structures Aa
, Bb
, Cc
, which inherit one common structure ABC
and classes A
, B
, C
, which inherit structures Aa
, Bb
, Cc
. Each of them contains virtual methods. There is a common part and "unique" part in classes A
, B
, C
. Here is a code:
#include <iostream>
using namespace std;
struct ABC {
virtual ~ABC() {
};
virtual void printABC() = 0;
virtual void someMethod() = 0;
};
struct Aa: ABC {
virtual void printA() = 0;
};
struct Bb: ABC {
virtual void printB()=0;
};
struct Cc: ABC {
virtual void printC()=0;
};
class A: public Aa {
public:
A() {
}
virtual ~A() {
}
//unique part
void printA() {
cout<<"A!"<<endl;
}
//common part
void printABC() {
cout<<"ABC"<<endl;
}
void someMethod() {
cout<<"ABC!"<<endl;
}
};
class B: public Bb {
public:
B() {
}
virtual ~B() {
}
//unique part
void printB(){
cout<<"B!"<<endl;
}
//common part
void printABC() {
cout<<"ABC"<<endl;
}
void someMethod() {
cout<<"ABC!"<<endl;
}
};
class C: public Cc {
public:
C() {
}
virtual ~C() {
}
//unique part
void printC(){
cout<<"C!"<<endl;
}
//common part
void printABC() {
cout<<"ABC"<<endl;
}
void someMethod() {
cout<<"ABC!"<<endl;
}
};
It seems to be not optimal. Is it possible to make template for A
, B
, C
and how? Or, maybe, it's better to make new class with all common methods within or make hierarchy. Suppose, we cannot change structures: ABC, Aa, Bb, Cc, but we can change classes A, B, C. Which method is more elegant and how to realize it better?
Upvotes: 0
Views: 148
Reputation: 304
It depends on how you will use your structures and which kind of polymorphic behavior you need.
if your willing is to handle A B C objects from ABC pointers so then you truly need this hierarchy for example : if you have and array of ABC pointers pointing to different A B C objects .
if you only have a common code within different class then templates will be better and a single function will be enough for you treatment
template <class X>
void someMethod (X object) {
}
And if you cannot change ABC ,Aa ,Bb ,Cc structure then you don't have other choice then to use hierarchy .
Upvotes: 2
Reputation: 28241
Stuff the common code into a dedicated base class:
struct ABC_CommonCode
{
//common part
void printABC() {
cout<<"ABC"<<endl;
}
void someMethod() {
cout<<"ABC!"<<endl;
}
};
class A: public Aa, public ABC_CommonCode
{
...
};
class B: public Bb, public ABC_CommonCode
{
...
};
This will work if the real code in printABC
doesn't need access to members of your classes. However, your code is just an example, and your real code is probably more complicated than that. If printABC
needs to work with an instance of class A
, you can use e.g. delegation:
template <class A_or_B_or_C>
struct ABC_CommonCode
{
static void printABC(A_or_B_or_C& object) {
cout << "ABC = " << object.GetABC();
// 100 lines of other code
}
};
class A: public Aa
{
// common part
// repeated in each class, but it's just 3 lines of code
// regardless of actual implementation
void printABC() {
ABC_CommonCode<A>::printABC(*this);
}
};
You might also want to read about CRTP, but I am not sure it's warranted here.
Upvotes: 1