KorbenDose
KorbenDose

Reputation: 1263

How to avoid pass-through functions

A class I am currently working on has a member of some type that defines various functions. My class shall be a wrapper around this type for various reasons (e.g. make it thread-safe). Anyway, some of the type's functions can just be passed through, like the following:

class MyClass {
  // ... some functions to work with member_

  /* Pass through clear() function of member_ */
  void clear() {
    member_.clear()
  }

private:
  WrappedType member_;
};

This is not that bad, plus I get the flexibility of easily being able to add further functionality to MyClass::clear() in case I need it. Nevertheless, if I have a few of these pass-through functions, it bloats MyClass and for me makes it harder to read.

So I was wondering if there is a nice one-line-way (besides writing the upper definition into one line) of passing through WrappedType's member functions, much like making base class members available:

/* Pass through clear() in an easier and cleaner way */
using clear = member_.clear; // Unfortunately, this obviously doesn't compile

Upvotes: 2

Views: 1311

Answers (1)

Vittorio Romeo
Vittorio Romeo

Reputation: 93264

Privately inherit from your base class and expose a subset of the interface with the using keyword:

class MyClass : private WrappedType 
{
public:
    using WrappedType::clear; 
};

Upvotes: 6

Related Questions