0

I‘m trying to pass a pointer to an object which class inherits from another class to a function and then access the member functions of it.

This is the base class:

class Base
{
public:
  virtual int funcA(int paramA) = 0;
}

This is the derived class:

class Derived : public Base
{
public:
  Derived();
  ~Derived();
  int funcA(int paramA);
}

Derived::Derived() {}
Derived::~Derived() {}
Derived::funcA(int paramA)
{
  // SOMETHING
}

And the function should look something like this:

template<class T> // T should only be types that are derived from Base
void funcB(T* object)
{
  object.funcA(10);
}

I‘ve already tried many different things but none of them compiled because I‘m trying to access a function of a template.

M Qwadezo
  • 9
  • 1
  • 6

1 Answers1

1

First of all the definition of Derived::funcA(int paramA) lacks the return argument int and having a return type int it will have to return something e.g. paramA. For the sake of clarity that you want to override the function of the Base class I would also mark funcA as override or final. Secondly by passing a pointer to funcB you will have to access the member function with the arrow operator -> instead of the dot operator .. The cleanest way of implementing this would be to activate the function with SFINAE (std::enable_id) only if the template argument T inherits from Base. For C++17 this would look like

template<class T>
std::enable_if_t<std::is_base_of_v<Base, T>, void>
funcB(T* object) {
  object->funcA(10);
  return;
}

For C++11 something like

template<class T>
typename std::enable_if<std::is_base_of<Base, T>::value, void>::type
funcB(T* object) {
  object->funcA(10);
  return;
}

should do the job. Try it here.

2b-t
  • 2,414
  • 1
  • 10
  • 18
  • does this also work with member functions? – M Qwadezo May 13 '21 at 16:33
  • @MQwadezo What error message do you get with MSVC? – 2b-t May 13 '21 at 16:33
  • the problem wasn't msvc, the problem is that in my real code, funcB is a member function – M Qwadezo May 13 '21 at 16:33
  • I see. It should not be an issue with `funcB` is a member function as well. What kind of error message do you get? – 2b-t May 13 '21 at 16:34
  • i don't know exactly how the error is in english, the error-code is ```LNK2019```. – M Qwadezo May 13 '21 at 16:35
  • Have you defined the function in a `cpp` file? `LNK2019` is typically connected to implementing a template function in a `cpp` file. For a template the declaration as well as the definition should go into the header. – 2b-t May 13 '21 at 16:39
  • i put the definition in the header and it works now! but why must the definition be in the header file? – M Qwadezo May 13 '21 at 16:44
  • It is like this with templates. A template is like a form that is filled out, the template type `T` is a placeholder: Whenever the compiler encounters an appropriate template argument it will insert it as the template parameter and create a new version of this function. In C++ each `.cpp` file is processed independently while the header files act like table of contents. So each `.cpp` file has to be able to fill out this form by its own. If you put the declaration into a `.cpp` another `.cpp` can't instantiate its required version of this function. – 2b-t May 13 '21 at 16:56
  • This means if you put it into a `.cpp` file it works as long as you either instantiate it inside the `.cpp` or you only use the template functions from that particular `.cpp`. I have written a comment about it some time ago with some links if you want to have a look: https://stackoverflow.com/questions/67127148/templates-coding-only-in-header-file/67170870#67170870 – 2b-t May 13 '21 at 16:57