Vladimir Yanakiev Vladimir Yanakiev - 7 months ago 31
C++ Question

passing interface or reference to a function as parameter

I have wrapper class and I have pass its object to a function.

class wrapper
/// some functionality

What will be better to pass as reference to the class like

fun(wrapper & ob)

or to new Interface that is inherited by wrapper like:

class Interface
class wrapper:public Interface
fun(Interface& ob)


If you want your function to only accept instances of the wrapper class, use

fun(wrapper& ob)


fun(const wrapper& ob)

if you do not intend to modify it.

If you want your function to accept anything that implements the Interface class (only really useful if there are more classes than just wrapper inheriting from Interface), then use:

fun(Interface& ob)


fun(const Interface& ob)

The choice depends on the use-case.