LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-24-2006, 12:53 PM   #1
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Rep: Reputation: 15
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
 
Old 01-24-2006, 01:32 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 01-24-2006, 01:43 PM   #3
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Original Poster
Rep: Reputation: 15
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
 
Old 01-24-2006, 02:03 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
In C++ the dictionary is implemented in the map or multimap template classes
 
Old 01-24-2006, 02:10 PM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.

Last edited by graemef; 01-24-2006 at 02:11 PM.
 
Old 01-26-2006, 12:44 AM   #6
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Original Poster
Rep: Reputation: 15
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
 
Old 01-26-2006, 04:56 AM   #7
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
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.
 
Old 01-26-2006, 03:08 PM   #8
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Original Poster
Rep: Reputation: 15
thanks for ur help
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ and inheritance question niteshadw Programming 7 08-08-2005 04:43 PM
Template class with a template member... Nicholas Bishop Programming 3 02-21-2005 08:27 PM
python inheritance towlie Programming 3 09-23-2004 12:13 AM
inheritance kalleanka Programming 4 02-29-2004 07:19 PM
sub-shell inheritance drum1975 Linux - General 4 03-11-2002 03:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration