Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exposing member variables methods in C++

Tags:

c++

c++11

I have a simple Object-Components design. Something like:

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

Is it possible to expose as public: methods of Objects1 some methods from ComponentK without creating a method in Object1 that calls internally ComponentK method?

I need a simple way of doing this because it's quite annoying to write a function every time I want to expose some ComponentK method.

like image 213
Mircea Ispas Avatar asked Apr 09 '13 06:04

Mircea Ispas


1 Answers

Not directly, no. You could use private inheritance to inherit from the components instead of aggregating them (note that private inheritance does not express is-a), and then publish some of their member functions by using. Something like this:

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

I'm not saying it's necessarily the best way to do it, but it's a way.

like image 191
Angew is no longer proud of SO Avatar answered Oct 03 '22 12:10

Angew is no longer proud of SO