'sibling' classes?

Hello,

Is it possible to share the part derived from a base class amongst derived classes?

I am attempting something like this:
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
//A 'shared' base class
class BaseClass
{
protected:
	int varA, varB;
};


//A first derived class (there are various more like this)
class DerivedClass_1 : public BaseClass
{
public:
	void func_1();
};

int DerivedClass_1::func_1()
{
	//do something involving varA, varB
	varA++, varB++;
}


//A second derived class, which refers to functions from several other classes derived from BaseClass
class DerivedClass_2 : public BaseClass
{
public:
	void func_2();
};

DerivedClass_2::func_2()
{
	int result1 = varA + varB;
	
	//adjust varA and varB
	func_1(); // --> this is the part giving me trouble
	
	int result2 = varA + varB;
	
	if(result1 != result2)
		cout << "Something changed" << endl;
	else
		cout << "No change today" << endl;
}

(The base class is some kind of data structure, and the derived classes operations on that data.)

So, basically what I'd like is to access functions of a separate class whilst making sure it addresses the same data as the class from which I'm calling that function, without having to explicitly reset all parameters that I may have adjusted (and which derive from the base class). And there are actually various classes like the DerivedClass_1 that I would like to access that way from DerivedClass_2, so I can't just make DerivedClass_2 a derived class of DerviedClass_1 either, as it leads to compiler errors of the type:
error: reference to varA is ambiguous
BaseClass.h:4: error: candidates are: int BaseClass::var1
BaseClass.h:4: error: int BaseClass::var1


If you want both DerivedClass_1 and DerivedClass_2 to have access to the func_1() function you should make func_1() a member of BaseClass.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Data;

class Operation{
protected:
   shared_ptr<Data> data; //'shared' data
};


class Derived2: public Operation{
   void foo(){
      Derived1 asdf(data);
      asdf.bar();
      asdf.foo();
      asdf.func(); //to access a method you need an object.
   }
};


without having to explicitly reset all parameters that I may have adjusted
¿eh? ¿why reset?
I can't just make DerivedClass_2 a derived class of DerviedClass_1 either, as it leads to compiler errors
You made a mistake. Show the code that produces the error.
You made a mistake. Show the code that produces the error.

The code producing that error is:
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
class BaseClass
{
protected:
	int varA, varB;
};

class DerivedClass_1a : public BaseClass
{
public:
	void func_1a();
};

class DerivedClass_1b : public BaseClass
{
public:
	void func_1b();
};

class DerivedClass_2 : public DerivedClass_1a, public DerivedClass_1b
{
public:
	void func_2();
};

void DerivedClass_2::func_2()
{
	int result = varA+varB;
	//Stuff involving func_1a() and func_1b()
}



I still have to think about your suggestion not to derive these classes DerivedClass_1x from my BaseClass, and use a pointer instead. I think it might do what I want (and it certainly wouldn't cause the abovementioned compile time errors), but I need to check if it is possible to implement it in a way that also respects a couple of other demands / restrictions.

For reference, the structure that I had in mind was something like this:
http://img829.imageshack.us/img829/4834/classstructure.jpg
The DerivedClass_1x's do not need to know about each other or any DerivedClass_2x; but DerivedClass_2x methods should have access to members of various DerivedClass_1x's, and it shouldn't matter in what way I manipulate BaseClass members (through which DerivedClass_1x, or preferably also directly), and these manipulations should be transparent through all derived classes. (I hope that makes sense...)


EDIT:
Hm, it looks like declaring adding a few instances of 'virtual' in the right places should solve (at least most of) my problems...
1
2
3
4
5
6
7
8
9
10
11
class DerivedClass_1a : virtual public BaseClass
{
public:
	void func_1a();
};

class DerivedClass_1b : virtual public BaseClass
{
public:
	void func_1b();
};
Last edited on
Look for `diamond problem' (virtual inheritance)
Also, check out `composite'
1
2
3
class Derived2: public Operation{
   container<Operation> things_that_i_can_do;
};
you have to use virtual inhritance:
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
class BaseClass
{
protected:
	int varA, varB;
};

class DerivedClass_1a : virtual public BaseClass
{
public:
	void func_1a();
};

class DerivedClass_1b : virtual public BaseClass
{
public:
	void func_1b();
};

class DerivedClass_2 : virtual public DerivedClass_1a, virtual public derivedClass_1b
{
public:
	void func_2();
};

void DerivedClass_2::func_2()
{
	int result = varA+varB;
	//Stuff involving func_1a() and func_1b()
}

Now DerivedClass_2 will have only one copy of BaseClass, so the refernces to varA and varB won't be ambigous!
EDIT: just saw your edid, you figured it out :D
Last edited on
Topic archived. No new replies allowed.