Consider the following, simplified facade pattern:
class Foo {
public:
int times;
int eval(const int val) { return val*times; }
};
class Bar {
Foo foo;
public:
Bar(const Foo& f) : foo(f) {}
double eval(const double val) { return val * foo.times; }
};
Obviously, an instance of Bar is only required to evaluate a special (i.e. double-valued) interpretation of Foo's eval() method. A Bar won't have any other members except the foo it forwards to.
For safety reasons I have not used a const
reference or a pointer
inside Bar (I just don't know yet if at some point a Bar instance might escape from a stack
frame, so resource management is important).
My question here is two fold:
- Can the
C++
compiler possibly detect that Bar is merely a facade and "inline" the member access? - Is there a (safe) way to prevent copying of the passed object?
For example: