Skip to content

Instantly share code, notes, and snippets.

@sahib
Created October 23, 2011 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sahib/1307305 to your computer and use it in GitHub Desktop.
Save sahib/1307305 to your computer and use it in GitHub Desktop.
// Kurzes Beispiel wie man die Member einer Template Klasse
// doch noch auslagern kann - vor Gebrauch in Datein aufsplitten :-)
// Kompilieren mit: g++ template_main.cc template.cc template.hh -Wall -Wextra
//
// Das ganze ist etwas unschön. Was lernen wir daraus?
// Das gleiche wie bei 'new' templates nur benutzen wenn's
// wirklich begründet ist.
// ~c3
//===================================
// template.cc
//===================================
#ifndef MY_TEMPLATE_CC
#define MY_TEMPLATE_CC
/* Inkludeire die Klassendef. */
#include "template.hh"
#include <iostream>
/* Dieses template <class T> muss man leider immer mit hinschreiben */
template <class T>
MyTmplClass<T>::MyTmplClass(T value)
{
/* Ausgeben (das klappt nur solange es der typ unterstützt!
* Hier haben wir ein int - deshalb geht es problemlos
* */
std::cout << "Truth =" << value << std::endl;
/* Auch this funktioniert noch */
this->a_member = value;
}
#endif
//===================================
// template.hh
//===================================
#ifndef MY_TEMPLATE_H
#define MY_TEMPLATE_H
/* Eine dummy template klasse */
template <class T>
class MyTmplClass
{
private:
T a_member;
public:
MyTmplClass(T param);
};
/* Die defintion der funktionen hier einfügen.
* include fügt sie wirklich beim kompileieren ein.
*/
#include "template.cc"
#endif
//===================================
// template_main.cc
//===================================
#include <cstdlib>
#include "template.hh"
int main(void)
{
MyTmplClass<int> instance(42);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment