C++ "Object" class

18,133

Solution 1

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;

Solution 2

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

Share:
18,133
Admin
Author by

Admin

Updated on June 06, 2022

Comments

  • Admin
    Admin almost 2 years

    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.

  • dmckee --- ex-moderator kitten
    dmckee --- ex-moderator kitten almost 12 years
    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
  • Mooing Duck
    Mooing Duck almost 12 years
    While this is correct, I feel like you're recommending using an Object class, when you should be recommending templates instead.
  • Luchian Grigore
    Luchian Grigore almost 12 years
    @MooingDuck that's is what I'm recommending. I don't see a template solution, please add one if you do.
  • Luchian Grigore
    Luchian Grigore almost 12 years
    @MooingDuck :)))) I missunderstood the question completely. I though he wanted a std::list that can hold multiple objects of different types, like std::list<Base*>.
  • Mooing Duck
    Mooing Duck almost 12 years
    @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)
  • Luchian Grigore
    Luchian Grigore almost 12 years
    @MooingDuck I think I (possibly mistakenly) assume that people implement their own lists and vectors for purely educational purposes.
  • Mooing Duck
    Mooing Duck almost 12 years
    @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.