Template classes

Started by
26 comments, last by Jiia 18 years, 11 months ago
Can someone point me in the direction of a tutorial on this? I managed fine when all my class members were defined inside the class, but when I tried to define them in the .cpp file I could not get anything to compile, even following the guide in the MSVC++ help. Or does anyone have a simple example - it was actually to template some classes for a D3D FVF Vertex structures rather than hard-code it everywhere.
Advertisement
Can you post your problem code?

To make sure I understand what you are doing... Do you have the class declarations in a header file and the function / member definitions in a cpp file? If so, is your cpp including your header? Just trying to get an understanding of what you are doing.

Also, can you post the compile errors?
That's how templates work. The function implimentation (what normally goes into the cpp file) needs to be visible from anywhere you use that object. That usually means all of the template member functions need to be inlined in the header.

Think of templates like #define macros. You only define something in a cpp file if that is the only place it's used.
Quote:Original post by Jiia
That usually means all of the template member functions need to be inlined in the header.


Thats not actually true, unless the compiler used supports the standard c++ keyword export which hardly any does except for one compiler i know of does all template definitions need to be in the same translation unit that does not mean it must be defined inline.
Quote:Original post by snk_kid
Thats not actually, unless the compiler used supports the standard c++ keyword export which hardly any does except for one compiler i know of does all template definitions need to be in the same translation unit that does not mean it must be defined inline.

Yep. By inlined in the header, I simply meant defined there. There's really very little reason to ever add the inline keyword to a header-implimented member function.
Well I don't have any code now because I got fed up and reverted to the fixed-type version.
But in my .h file I had something like:
template <class T> class MyClass{private: T *mData; const int mDataSize;public: MyClass(int size); ~MyClass();};
Then in the .cpp file I might have:
#include "MyClass.h"template <class T> MyClass<T>::MyClass(int size) : mDataSize(size), mData(0){ mData = new T[mDataSize];}template <class T> MyClass<T>::~MyClass(){ if(mData)  delete [] mData;}
This .cpp file compiled fine, but as soon as I tried to create a templated version of it I got errors:
 struct VERTEX{ D3DXVECTOR3 pos; DWORD ambient;};MyClass<VERTEX> object(100);
Is this a problem with the data type I'm using not supporting some fucntionality, or is my syntax incorrect?
Like I said before, any file where you use your template class needs to have access to all of the template classes' methods. snk_kid's "not true" remarks were just concerning my use of the word "inline". He just likes to complain.

So whatever file contains MyClass<VERTEX> object(100);, also needs to contain the template methods. If you don't know of any other way to do this, simply include your constructor and destructor in the template header.
To clarify:

Quote:Original post by d000hg
Well I don't have any code now because I got fed up and reverted to the fixed-type version.
But in my .h file I had something like:
template <class T> class MyClass{private: T *mData; const int mDataSize;public: MyClass(int size); ~MyClass();};
Then in the .cpp file I might have:


Don't! I.e., don't have a .cpp file for this implementation. Instead:


template <class T> class MyClass{private: T *mData; const int mDataSize;public: MyClass(int size) : mDataSize(size), mData(0){ mData = new T[mDataSize]} ~MyClass(){ if(mData)  delete [] mData;}};


(Although, assuming pointer ownership, your implementation could be simplified further by making use of std::auto_ptr or something like it. ;) Well, actually I guess std::auto_ptr isn't really any good for an array allocation... :s )

Alternately, you could keep the .cpp file, but #include it at the end of your .h. Ugly, yeah? :/ Them's the breaks with templates, though. (Often such an "included template implementation" file will be given an extension like .i or .inl instead of .cpp .)
Oh, OK. I thought Templated classes were just special classes. So actually they're a dirty hack? If I define the function body in the class declaration are they still inline functions - I probably don't want that.
Quote:Original post by d000hg
Oh, OK. I thought Templated classes were just special classes. So actually they're a dirty hack? If I define the function body in the class declaration are they still inline functions - I probably don't want that.


It is true that having the function body inside the class declaration is treated as a suggestion for inlining. However, the keyword in this sentence is "suggestion" (the same goes for the inline keyword too). Whether or not the compiler will inline a function which has been suggested to it, is up to the compiler (and a compiler may as well decide to inline a function which was not marked with inline).
If you don't want even that suggestion, specify the functions in the header but not inside the class declaration or #include your cpp implementation file for that template class in the header. I personnaly dislike that, but if you absolutely want to split it up into .h and .cpp...

This topic is closed to new replies.

Advertisement