Welcome! Log In Create A New Profile


Template classes as ADT's

Posted by valkeye 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
avatar Template classes as ADT's
July 17, 2008 10:24AM
Hi all,

An interesting read.. (taken from Bruce Eckel's Thinking in C++, volume 1)

Header files
Even if you create non-inline function definitions, you’ll usually
want to put all declarations and definitions for a template into a
header file. This may seem to violate the normal header file rule of
“Don’t put in anything that allocates storage,” (which prevents
multiple definition errors at link time), but template definitions are
special. Anything preceded by template<...> means the compiler
won’t allocate storage for it at that point, but will instead wait until
it’s told to (by a template instantiation), and that somewhere in the
compiler and linker there’s a mechanism for removing multiple
definitions of an identical template. So you’ll almost always put the
entire template declaration and definition in the header file, for ease
of use.

There are times when you may need to place the template
definitions in a separate cpp file to satisfy special needs (for
example, forcing template instantiations to exist in only a single
Windows dll file). Most compilers have some mechanism to allow
this; you’ll have to investigate your particular compiler’s
documentation to use it.

Some people feel that putting all of the source code for your
implementation in a header file makes it possible for people to steal
and modify your code if they buy a library from you. This might be
an issue, but it probably depends on the way you look at the
problem: Are they buying a product or a service? If it’s a product,
then you have to do everything you can to protect it, and probably
you don’t want to give source code, just compiled code. But many
people see software as a service, and even more than that, a
subscription service. The customer wants your expertise, they want
you to continue maintaining this piece of reusable code so that they
don’t have to – so they can focus on getting their job done. I
personally think most customers will treat you as a valuable
resource and will not want to jeopardize their relationship with
you. As for the few who want to steal rather than buy or do original
work, they probably can’t keep up with you anyway.

avatar Re: Template classes as ADT's
July 17, 2008 10:41AM


Post no.7 was an interesting idea. What do you guys think?

avatar Re: Template classes as ADT's
October 02, 2008 02:04PM
valkeye Wrote:
> Another.
> http://bytes.com/forum/thread478121.html
> Post no.7 was an interesting idea. What do you
> guys think?
> -Valkeye

After I moved the class definitions to a separate cpp file, I tried this and the only thing I can see this doing, is fixing the link error messages, if you haven't created as a project. Tested with linkedListType and orderedLinkedListType. Another solution is to place the include to the implementation file, with the header file include, in the client program. The first method is a lot better though.

Malik does mention this on page 114, but does not show how. [cannot find]

PS: Does not fix the friend function in templates problem. Still need the place those two lines before the class as mentioned in another thread. ie
template<class Type> class linkedListType;
template<class Type> ostream& operator<<(ostream&, const linkedListType<Type>& );
Oh. and also the <> in the friend function declaration. This tells the compiler, this function is a template function. The compiler does not know this, even though declared in a template class.

Sorry, only registered users may post in this forum.

Click here to login