0

How can I delegate all methods of a part object to the composite class without using a get() method and call it on the Composition object:

Example:

struct Part {
  void f() {};
  int g(double &v) { v = 2; return 3; }
};

struct Composition {
  Part part;
};

The Composition class should be able to be used in this way:

Composition p;
p.f()
int a = p.g(2.0)

Additionally I want use boost::bind in this way:

boost::bind(&Composition:f, this) 

2 Answers2

0

You need to use the inheritance property of structs. See Struct inheritance in c++,

So, you could do

struct Part {
  void f() {};
  int g(double &v) { v = 2; return 3; }
};

struct Composition : Part{

};
Community
  • 1
  • 1
tornesi
  • 269
  • 2
  • 8
0

First of all, if you want a class to have all the behavior of another partial class, consider inheritance instead of composition.

If you insist using composition, then you would have to manually write a set of delegate functions. For example:

struct Composition {
  Part part;
  void f() {return part.f()}
  int g(double &v) { return part.g(v); }
};

Off topic: Function Part::g uses a reference to double so the line int a = p.g(2.0) is not going be be compiled any way.

Lucien
  • 134
  • 5