24

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.

user142852
  • 341
  • 1
  • 2
  • 8
  • 8
    In C++, you would do this using templates. If you wanted to have different types in the same data structure, then you'd use inheritance. – Mysticial Jul 31 '12 at 19:22
  • 1
    Can you post some of your code for this? Are you talking about using C++ templates to allow for a linked list of a specific type? – Tyler Ferraro Jul 31 '12 at 19:22
  • 1
    If C++ had handy classes like that then it wouldn't be called C++. :) – user541686 Jul 31 '12 at 19:35
  • possible duplicate of [From Java Object class to C++](http://stackoverflow.com/questions/2648756/from-java-object-class-to-c) – Mooing Duck Jul 31 '12 at 19:35
  • Honestly I don't see the relationship between "generic base class" with the "linked list... work for only one type, but not multiple". Do you mean you only want to write the linked list once and be able to use it with different type? – Adrian Shum Aug 01 '12 at 02:28

2 Answers2

29

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;
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 2
    Or you can use one of the many existing libraries with God objects. In any case there is a good chance that you will contemplate a messy and painful suicide when you being to comprehend the full implications of this decision. *// likes everything **else** about ROOT* – dmckee --- ex-moderator kitten Jul 31 '12 at 19:33
  • 1
    While this is correct, I feel like you're recommending using an `Object` class, when you should be recommending templates instead. – Mooing Duck Jul 31 '12 at 19:34
  • @MooingDuck that's is what I'm recommending. I don't see a template solution, please add one if you do. – Luchian Grigore Jul 31 '12 at 19:40
  • @MooingDuck :)))) I missunderstood the question completely. I though he wanted a `std::list` that can hold multiple objects of different types, like `std::list`. – Luchian Grigore Jul 31 '12 at 19:46
  • @LuchianGrigore: He thinks he does, but I think a good answer should recommend something like `std::list` _instead_ of what he's trying to do, if possible. (I acknowledge that it might not match his needs) – Mooing Duck Jul 31 '12 at 19:48
  • @MooingDuck I think I (possibly mistakenly) assume that people implement their own lists and vectors for purely educational purposes. – Luchian Grigore Jul 31 '12 at 19:50
  • @LuchianGrigore: If you reread my comments, I was very careful to say "recommend templates" "point him at list" "something like list", and I was attempting to avoid "use list" for that very reason. – Mooing Duck Jul 31 '12 at 20:29
4
class Object{
protected:
    void * Value;
public:



template <class Type>
void operator = (Type Value){
        this->Value = (void*)Value;
}

template <>
void operator = <string>(string Value){
        this->Value = (void*)Value.c_str();
}

template <class Type>
bool operator ==  (Type Value2){
        return (int)(void*)Value2==(int)(void*)this->Value;
}

template<>
bool operator == <Object> (Object Value2){
        return Value2.Value==this->Value;
}

template <class ReturnType>
ReturnType Get(){
    return (ReturnType)this->Value;
}

template <>
string Get(){
    string str = (const char*)this->Value;
    return str;
}

template <>
void* Get(){

    return this->Value;
}

void Print(){
    cout << (signed)this->Value << endl;
}


};

Then make a subclass of it

Thomas Eding
  • 35,312
  • 13
  • 75
  • 106
MuffinMan
  • 41
  • 1
  • 1