Class template <-> compile/link

Hi,

Normally I code a class into 2 files, a h-file and a cpp-file. The class is compiled separate.
When the class is used in a program, the h-file is included, and the program is compiled separate.
At the end, to make a runnable, all the separate compiled stuff is linked together.

Now I have a class template which I want to use to generate (let's say) 100 classes. Each of those classes will use the general template, but will also have a number of dedicated stuff (public getters/setters) to that one class.

So I made an include-file for the template. And for the first class a class1.cpp file.
I include the template-file in the class1.cpp file, create the class with use of the template, add the dedicated getters/setters ... and compile it. The compile goes oke.
I will do the same for class2 to class100.

Now I want to use these classes in other programs ... using the same compile/link technique stated at the beginning. If I want to use class1 in programX, I will need a h-file for class1 ... and subsequently for all 100 classes (made with the template).

Now, suppose later on the class-template gets a new public method ... I will have to add this method to all the 100 h-files to be able to use it.

My questions are:
1. is above thinking correct ?
2. are template-classes (as described above) suited for the separate compile, and link later, technique ?
3. if 2. is answered with yes, is there a good way to prevent editting 100 class h-files when public/private methods are added to the template ?

Thanks.
I don't get it. How are the 100 classes related to the template class? Are they template specializations?
1. is above thinking correct ?

Have you even tried 1 class this way? It sounds like you are borderline breaking the ODR or Linker issues by the verbage you've used, but maybe not.

2. are template-classes (as described above) suited for the separate compile, and link later, technique ?
You can do this, but not like non-template classes. You have to actually instantiate the template to do this OR only use said template in that translation unit and that translation unit alone. Once you cross translation units you will have issues.

3. if 2. is answered with yes, is there a good way to prevent editting 100 class h-files when public/private methods are added to the template ?


If by writing 100 classes you mean 100 specialization then no, there is no good way you will have to implement them. Otherwise, if you don't use the function you will not need to. Here is an example below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template<typename Derived>
class Base{
   public:
   void Function1(){
       static_cast<Derived *>(this)->DoFunction1();
   }
   void Function2(){
       static_cast<Derived *>(this)->DoFunction2();
   }
};

class MyClass: public Base<MyClass>
{
   private:
   void DoFunction1(); //Pretend implemented
   //Notice no DoFunction2
};

main{
    Base<MyClass> obj;
    obj.Function1();  //Calls derived DoFunction1();
}


Function2() is never called, this compiles and runs just fine, once you make a call to Function2() in your code, you will now get compile errors.

disclaimer: There may be syntax errors in the above code, it's the point I'm trying to make that matters.
Last edited on
Yes, they are specializations (different structures).

Yes, I tried it. When you want to use a class, that's compiled separate, then you need a h-file.
So they only way to handle this it to not have a h-file, but just include the template, and the class that's made with it, in the source where you want to use them ... so that they are compiled in one compile. That works fine by the way.
Topic archived. No new replies allowed.