oh really? That's weird. Good to know,
Thanks
Main Topics
Browse All TopicsHi,
Can anyone compare C++ templates to Java generics? I just wrote a program using java generics and they seemed a little easier to use than templates (of course I did some really basic stuff). For instance in java I defined a node wrapper class like:
public class CNode<T> {
// The generic data we're wrapping around.
T m_data;
// Pointer to the next node element, if any.
CNode<T> next;
// Pointer to the previous node element, if any.
CNode<T> prev;
/** Creates a new instance of CNode */
public CNode(T t)
{
m_data = t;
next = null;
prev = null;
}
}
which is pretty simple, my C++ version I had to figure out that anything that touched my template had to be moved into the header file (if I did that right) and was a little annoying because it broke my usual style of always having the function bodies in corresponding cpp files.
Anyone care to comment on their experience(s) between the two?
Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
AFAIK the main difference is around the way the compiler works with generics and how the underlying code is created. In c++ the templates are actually boiled down into real code against the given object type, so for example a vector of type char and a vector of type string will result in two classes in your program... i tend to think of templates in c++ (albeit loosely) as clever pre-processor macros...
in Java, however, the compiler, knowing the templated type, simply inserts the right cast for you when adding and extracting the type from the generic collection etc... the reason it can do this is because everything in Java inherits from Object... this results in a slight run-time overhead but only a single instance of a generic class within your code... I would say that Java offers code-time type safety as the compiler will warn you if you attempt to put an 'Apple' in Vector of 'Cars' for example
from a programming point of view there is little difference as both C++ and Java allow you to do pretty much the same... or at least i have not noticed a difference in my use of these language features.
-P
Business Accounts
Answer for Membership
by: jkrPosted on 2006-12-20 at 08:51:37ID: 18174439
>>my C++ version I had to figure out that anything that touched my template had to be moved into the header
>>file (if I did that right)
Yes, but that is not a problem of C++ (the standard allows separating template declarations and definitions), it's just that hardly any compiler out there supports that :o(