Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Template Questions (http://www.programmingforums.org/showthread.php?t=12483)

King Feb 1st, 2007 7:43 PM

Template Questions
 
I just have a few questions about templates. I will just use the following class as an example to use for my questions:
:

template <class T>
class MyClass
{
public:
        MyClass() {};
        ~MyClass() {};
        void MyFunction(const T &);
};

template<class T>
void MyClass<T>::MyFunction(const T &value)
{
        // do something
}

Usually when I write a class I would put the implementation of the functions in a separate .cpp file, but when I do this I get compile errors. But if I put the implementation of the function in the same file as the class definition it works fine, why is that?
My second question is if I have to keep the implementation of my functions in the same file, would there be a point to still define it outside if the class definition like I have shown above, or should I just define it inside the class?
Thanks.

Eoin Feb 1st, 2007 9:01 PM

Too tired to phrase an intelligent answer to the first but this link seems to do a good job at explaining the details.

On the second question, well for your example it is largely just a matter of taste, but again the mentioned link goes more into the specifics.

bl00dninja Feb 2nd, 2007 3:44 AM

can't give any cool details...BUT i've been told that when you use templates the declarations and definitions need to be in one file.

the reason you find weird shit like this is that C++ is not a ground-up new language. it's a wrapper for C. so add-on crap like templates involves weird shit so they don't have to rewrite the whole compiler.

you just gotta live with it.

good luck!

King Feb 3rd, 2007 8:30 PM

Thanks for the info guys. Is there any difference at all if I define the function inline or outside of the class definition?


All times are GMT -5. The time now is 2:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC