jeudi 19 avril 2018

Complex object inside another one

I have the following class

class A {
private:
   B b;
public:
   A();
}

class B {
public:
   void foo();
   void bar();
   void foz();
   ........
}

B has a lot of methods. Sometimes is needed for a "customer" of class A to use method of class B. I could return a reference of B, but in this case I should return a const reference because returning a non-const reference of a private object is not good programming. If the reference is const, foo, bar and so on can't be called because they aren't const. So the only "clean" way seems to recreate the same interfaces in A using delegation to B. But this approach is not really good because I should recreate all the interfaces in A. As alternative I could set B as public in A, but it seems "strange" to me. What should I do in this case?

Aucun commentaire:

Enregistrer un commentaire