Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ "Object" class

Tags:

In Java, there is a generic class called "Object", in which all classes are a subclass of. I am trying to make a linked list library (for a school project), and I have managed it to make it work for only one type, but not multiple, so is there anything similar to that?

EDIT: I would post the code, but I don't have it on me at this time.

like image 550
user142852 Avatar asked Jul 31 '12 19:07

user142852


1 Answers

There's no generic base class in C++, no.

You can implement your own and derive your classes from it, but you have to keep collections of pointers (or smart pointers) to take advantage of polymorphism.

EDIT: After re-analyzing your question, I have to point out std::list.

If you want a list which you can specialize on multiple types, you use templates (and std::list is a template):

std::list<classA> a; std::list<classB> b; 

If you want a list which can hold different types in a single instance, you take the base class approach:

std::list<Base*> x; 
like image 81
Luchian Grigore Avatar answered Oct 21 '22 16:10

Luchian Grigore