Identifying member variable methods in C ++

I have a simple Object-Components design. Sort of:

 class Object1 : public Object { Component1 _comp1; Component2 _comp2; ... ComponentN _compN; }; 

Is it possible to set some methods from ComponentK as public: methods of Objects1 without creating a method in Object1 that calls the ComponentK method inside itself?

I need an easy way to do this, because it is very annoying to write a function every time I want to set some ComponentK method.

+4
source share
1 answer

Not directly, no. You can use private inheritance to inherit components instead of aggregating them (note that private inheritance does not express is-a ) and then publishes some of its using member functions. Something like that:

 class Object1 : public Object, private Component1, private Component2, ..., private ComponentN { public: using Component1::function1(); using Component1::function2(); ... using ComponentN::functionM(); }; 

I am not saying that this is the best way to do this, but it is a way.

+11
source

All Articles