Question about template class

Q6. Which of the following statements are true?

A. A class template cannot be derived from a non-template class. -Not true
B. Non-Friends are used exactly the same for template classes. - Not sure
C. A template class can be derived from a non-class template specialization. -Believe it to be true
D. A class template can be derived from another class template. - Not sure
E. You can define dynamic members in a template class. Each template specialization will have its own copy of a dynamic function member element. - Not true
A) Correct
B) What do they mean by "Non-friends"? Classes that aren't friends trying to access data/functions?
C) I don't think so. If you have a non-class template thing, how could you ever suddenly get a class out of it?
D) True (see many of the STL streams for example)
E) What do they mean by dynamic?
When they say dynamic I would think they are saying anything allocated. It's true for static but I don't think dynamic
Let me answer like this: there are only two conditions for deriving A from B
- both A and B must be visible accessible struct or class, whether template or not
- if B is a template, then the arguments of B must be supplied either directly or expressed in terms of the arguments of A, if A is also a template

Particularly, you can derive template classes from non-template classes and this is useful when you want to reduce the template code bloat.

You can derive non-template classes from template class instantiations, as long as you specify concrete types and values for the template arguments.

Regarding the friends, non-template and template classes can be friends of both non-template and template classes. Specifically, a non-template or template class can have template specialization as a friend, or all template specializations as friends. The latter is achieved by using template friend declaration, which depends on its own template argument.

I don't understand the dynamic stuff you mention. All information regarding templates is erased after compilation, and everything behaves as if there were no templates at all, so there is hardly anything dynamic. But the closest sense to dynamic in which you can use templates is to have a common abstract base class that defines polymorphic interface and to derive template class from it. All template instantiations of that class will be usable polymorphically through the base interface. Something else that may answer your question - template specializations can have custom data layout that has nothing in common with the other template specializations.

Regards
Topic archived. No new replies allowed.