C++ “Object” class

前端 未结 2 1134
死守一世寂寞
死守一世寂寞 2021-02-05 07:27

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

2条回答
  •  無奈伤痛
    2021-02-05 07:54

    class Object{
    protected:
        void * Value;
    public:
    
    
    
    template 
    void operator = (Type Value){
            this->Value = (void*)Value;
    }
    
    template <>
    void operator = (string Value){
            this->Value = (void*)Value.c_str();
    }
    
    template 
    bool operator ==  (Type Value2){
            return (int)(void*)Value2==(int)(void*)this->Value;
    }
    
    template<>
    bool operator ==  (Object Value2){
            return Value2.Value==this->Value;
    }
    
    template 
    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

    提交回复
    热议问题