LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Template inheritance (https://www.linuxquestions.org/questions/programming-9/template-inheritance-407467/)

allomeen 01-24-2006 12:53 PM

Template inheritance
 
Hello,
Is it possible to use a user defined Template or from STL as a base class?

I'm trying to inherit list template to a class i build..

Alaa G

graemef 01-24-2006 01:32 PM

Yes you can, but are you sure that is what you want to do? Most of the time a class will have an association with a template classes rather than a specialisation.

That is you include the list as a member variable rather than create a specialisation of it.

graeme.

allomeen 01-24-2006 01:43 PM

The thing is that i have many objects that will need a list and its methods. The object will have only other two functions, so that's why i thought about inheriting. Is it a bad idea to inherit a template? and how would you do it.

I have another question, do you know about dictionary object? Is there anything for C/C++, or do i have implement mine?

Thanks,
Alaa G

graemef 01-24-2006 02:03 PM

In C++ the dictionary is implemented in the map or multimap template classes

graemef 01-24-2006 02:10 PM

It's hard to give you an explanation on how "I would do it" since I don't really know what you want to do.

But say I have a class called student and I want to be able to hold lists of marks and modules enrolled in, then I would make listMark and listModule member variables of the student class. These could be lists of other classes, such as list<mark> and list<module> and I will probably have a class called cohort that contains a list of students.

There is a simple rule when deciding if it should be inheritance or not. When you describe the situation would you use the phrase "is a" (inheritance) or the phrase "has a" (association)...

A student is a mark ... ? No
A student has a mark ... Yes.

graeme.

allomeen 01-26-2006 12:44 AM

The reason that I want to do is that I have an Object that act like a map but with more functions. I got it to work and this how:

template <class Key, class Item>
class fooMap: public Foo, public map<class Key, class Item>
{
public:
void foo1();
....

};

I have a question, Do you know the difference between "template<class T>" and "template<typename T>"?

Alaa

enemorales 01-26-2006 04:56 AM

Quote:

I have a question, Do you know the difference between "template<class T>" and "template<typename T>"?
Using the first one you save some keystrokes, but the second is clearer, because using "class" could make you think that you cannot use built-in types, which is not true.

allomeen 01-26-2006 03:08 PM

thanks for ur help


All times are GMT -5. The time now is 12:24 AM.