0

I have class A which has to implement some functions. Since implementing one of them needs it's own data structures, I assumed A contain another class B, which has all needed data structures and functions. However, B is also need to use data structures and functions of A, as well. I used two classes calling each others using forward declaration. But there is still problems. For example, I need to make all data structures in A public, in order to B can access it. I tried using friend classes, but when I declare B as an abstract classes with sub-classes which implements B's functionalities, I need to make all data structures of A, as public. Because friend class doesn't work for inherited sub-classes, all data structures of A, needs to be public. This makes my design quite messy.

class B; 
class A{
protected: 
    int ds[100];
    B * b; 
public:
    a_func(){ 
        b->b_func(); 
    }
};


class A; 
class B{
    A * a; 
public: 
    b_func(){
        a->a_func(); 
    }
};

class sub_B:public B{
public: 
    b_func(){
        a->a_func(); 
        a->ds ...; 
    }
}

My question is: is there any alternative design?

I also tried making A an abstract class and class B implements a function of it, however, it doesn't conceptually makes sense to build an object of B, when I want an object of A.

NaSh
  • 665
  • 5
  • 16
  • 3
    Why do you need an alternative? Just move the implementation (function definitions) to separate translation units (`.cpp` files). – πάντα ῥεῖ Jul 04 '15 at 17:36
  • It's in different unit. everything is separated. However, making every data structure of A, as public, is not a good design. It also doesn't look like that A contains a B and B is an smaller unit, inside A. – NaSh Jul 04 '15 at 17:54
  • Use pure abstract interfaces instead. They don't necessarily need to be exposed publicly. May be this helps: [How can I remove/refactor a «friend» dependency declaration properly?](http://stackoverflow.com/questions/27492132/how-can-i-remove-refactor-a-friend-dependency-declaration-properly) – πάντα ῥεῖ Jul 04 '15 at 17:58
  • This sounds like an analogue to Java inner classes. (Which is not necessarily a bad thing.) – celticminstrel Jul 04 '15 at 18:13

1 Answers1

1

You don't have to provide member function definitions inside a class definition:

class A;
class B;
class A {
  // no need for public
  B * b;
  void a_funct(void);
};
class B {
  // no need for public here, too
  A * a;
  void b_funct(void);
};
// the following goes in a source file,
// otherwise you should mark it as inline
void A::a_funct() {
  b->b_funct();
}
void B::b_funct() {
  a->a_funct();
}

Note that above code serves only as example, in its current shape it's nothing but a fancy endless (recursion) loop.

Daniel Jour
  • 15,896
  • 2
  • 36
  • 63
  • If I don't put "public" it will be private. so I cannot access b_func or a_func. – NaSh Jul 05 '15 at 15:43
  • True, so your issue is not the forward declaration but rather the general coupling between those classes. Though nobody will be able to help you much with that if you don't provide more specific information. – Daniel Jour Jul 05 '15 at 18:55