Design Problem

I have an inheritance problem that looks like this:


base
/ \
/ \
derv1 derv2


I have some functions in base that I want to be usable by both derv1 and derv2. However, some of these functions iterator through a vector of derv1* or derv2* and access some functions inside of them. Is there a way I can make this work or am I stuck just making the functions virtual and copy/pasting the code into the derived classes?
You can move the function outside the class and add a template parameter to get the right iterator.
Then you make that function virtual and on each class call the function passing what you want as template argument
I might be a bit off the mark here - but is this the general idea(I know it does not mention iterators) - of what you want?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Base
{

  public:

  template <typename T>
  int doDerivedFunc(T& derived)
  {
    return derived.func();
  }


};


class Derived1  :public Base
{
public:
  Derived1():a(3){}  
  
  int func()
  {
    return a;
  }

private:
  int a;
};


int main(int argc, char* argv[])
{
	
  
  Derived1 D;

  Base B;

  cout << B.doDerivedFunc(D) << endl;

  return 0;

}
Topic archived. No new replies allowed.