3

I created a type, which is a list of priority queues of strings (not my idea, I have to do it):

typedef list<priority_queue<string>> L; L myList; 

Now I need to create a class M, which will inherit from this type. From what I acknowledged, I need to use templates to do so, but I still have no idea how to do it and I haven't found anything online.

How do I make a class inherit from a type?

7
  • 2
    Are these std::list, std::priority_queue, and std::string, or are they your own versions? If the former, why do you need to inherit from them? Inheriting from standard library types isn’t illegal, but a lot of people seem to consider it a bad idea. And why are you having trouble; it should be no different from any other inheritance. Commented Dec 5, 2017 at 16:41
  • @MikeMB You can’t inherit from an object like myList; only from a class type. Commented Dec 5, 2017 at 16:44
  • @DanielH Yes, they are from std. I need to inherit from them, cause it's my home task from school. We usually have to do very weird and useless things in order to learn new constructions and such. I just made it work, but I'm not sure if I did it right. Is it in a good style to put "typedef list<priority_queue<string>> L;" outside of main()? To be exact, before it? Commented Dec 5, 2017 at 16:44
  • @Daniel: Sorry miss read the post on my mobile. Of course you have to inherit from L Commented Dec 5, 2017 at 16:45
  • @Ania I wouldn’t use that typedef, but it might be useful depending on the rest of your code. If you do have it, though, it’s probably a good idea to give it a more descriptive name than L, at least something like QueueContainer which tells you that it contains queues. Commented Dec 5, 2017 at 16:46

1 Answer 1

4

It's as simple as:

class foo : L { // Note this is private inheritance }; 

The type alias is just a name for another type, not a type in its own right. Since std::list is a class template and L names a complete specialization, you can just do it.

Just be sure not to use it in any polymorphic way that involves deleteing a pointer to a std::list. It's not designed for it, since it lacks a virtual destructor.

Sign up to request clarification or add additional context in comments.

17 Comments

The problem is, the compiler doesn't see the L type, can I fully legally put it over the class definition, outside of main()?
@Ania - I don't know what your complete setup is. But as this minimal example shows, what I did works as is.
@StoryTeller but I can't then do L t=new foo(); t->thing(); delete t;, because list doesn't have a virtual destructor :(
Thank you! Might sound like a stupid/easy question, but I found it weird to demand from me to inherit from a type, I've never done it.
@Ania - It's not stupid at all. It's just something novices better not do until they understand all the implications. And you asking this question is most certainly not a silly thing.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.