0

i am struggling with the C++ template concept. What i have is something like that:

abstractClass.h:

namespace a {
  namespace b {
    template <typename RETURNTYPE, typename DATATYPE>
    class AbstractClass {

      protected:
        RETURNTYPE rtype;
        DATATYPE dtype;
        std::size_t countValues;

      public:
        explicit AbstractClass(DATATYPE _dtype, size_t _countValues):
          rtype(0),
          dtype(_data), 
          countValues(_countValues)  {

          }           
          virtual void process() = 0;
    };
  }   // namespace worker
}   // namespace eetneon

derivedClass.h

#include "abstractClass.h"

namespace a {
  namespace b {
    template <typename RETURNTYPE, typename DATATYPE>
    class DerivedClass : public AbstractClass<RETURNTYPE, DATATYPE> {

      public:
        DerivedClass(DATATYPE _data, size_t _countValues): 
          AbstractClass<RETURNTYPE, DATATYPE>(_data, _countValues) {
        }

        void process() override;

    };
  }   // namespace worker
}   // namespace eetneon

Now my question is: Is it possible to implement the void process() method within a *.cpp file or do I have to put the implementation in the header file, so the compiler can see it (I read something like that lately), because shit just got real, when i tried to put the implementation stuff away from the *.h file. Are there any sophisticated approaches to solve this problem? Everything i found until now seems a bit rough around the edges!

Thanks for your time and please excuse my horrible english.

Sincerely Hymir

Hymir
  • 811
  • 1
  • 10
  • 20
  • 1
    http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – πάντα ῥεῖ Dec 22 '16 at 17:38
  • yes it's possible, but only for a limited set of template arguments. Probably not what you want. – Richard Hodges Dec 22 '16 at 17:40
  • Thanks for the quick answers. I am just looking for a suitable way to keep the code clean and structured. I mean, defining all templated classes at some point doesn't seem like the idea behind the concept of templates does it? Furthermore filling my header files with implementation stuff is also not the way i want to go if there would be any alternatives! – Hymir Dec 22 '16 at 17:44
  • Take a look at "extern templates" for C++11. – ABu Dec 22 '16 at 17:53
  • Wait for concepts and put templates in the interface. – Guillaume Racicot Dec 22 '16 at 18:22
  • I will check the extern templates! Seems like a good solution. What do you mean with "put templates in the interface"? Can you give me a short example pls? – Hymir Dec 22 '16 at 18:36
  • @Peregring-lk if i got this straight, i have to declare the templated class in my main.cpp for instance like extern class DerivedClass and also have to declare it within the implementation file of this class like – Hymir Dec 22 '16 at 19:06
  • `template class DerivedClass`? – Hymir Dec 22 '16 at 19:08
  • @Hymir I have never used `extern template`s yet, but, under my understanding, you have to do exactly what you have written, because the `template class DerivedClass;` explicitly instantiate the template. – ABu Dec 22 '16 at 23:13
  • Ok! Thanks alot for all your help! – Hymir Dec 23 '16 at 10:34

0 Answers0