I have a supper class which includes some common functions. The problem i'm facing is, i want to move some similar codes from all derived classes into one function. But those similar code uses one type, which is derived-class related. That means each derived class has its own type definition.
My question, where the common function should be?
I have two ideals about the common function so far:
1. put it in the supper class, using template member function. According to its duty, it should belong to the super class as well.
The problem is, if i put the declare in the header of super class, and put its definition in the implement file of supper class. When invoking it in derived classes, caused LNK 2019 error in VS2010. To resolve that, i have to put the definition of the template function in the header of super class. It's quite bad.
Another solution is to add the .cpp of super class to the projects where derived classes are in, but that cause additional error since it will bring more unnecessary dependence.
2. Create another class/file to put the common function used by all derived classes. I don't think this is good as well. It's a strange structure.
So, any suggestion for this case will be appreciated.
Bellowing is the main structure i have.
1 2 3 4 5
|
// class super in project s
class super
{
// in super class, it cannot see typeC and typeD.
};
| |
1 2 3 4 5 6
|
// class derivedA in project da
class derivedA : super
{
// typeC is declared and defined in project da, and cannot move outside of it.
void func(typeC* c);
};
| |
1 2 3 4 5 6
|
// class derivedB in project db
class derivedB: super
{
// typeD is declared and defined in project db, and cannot move outside of it.
void func(typeD* d);
};
| |