C ++ design with static methods

I would like to define as class X with a static method:

class X
{
 static string get_type () {return "X";}
 //other virtual methods
}

I would like to force classes that inherit from X to override the get_type () method and return strings other than "X" (I'm glad if they just override get_type at the moment).

How should I do it? I know that I cannot have virtual static methods.

Edit: The question is not about type_id, but generally about a static method that should be overestimated. for example

class X {
 static int getid() {return 1;}
}
+5
source share
10 answers
template<int id>
class X {
public:
    static int getid() { return id; }
};

class Y : public X<2> {
};

You did not redefine the method, but you forced each subclass to provide an identifier. Caution: I have not tried this, there may be some subtle reason why this will not work.

+5

, , , , X::get_type();, DerivedClass::get_type() .. , , . , , , , , .

, , ( , " " ), . get_type :

#include <string>

struct X {}; 
struct Derived: X {};

template <class T> std::string get_type() {
    static_assert(sizeof(T) == 0, "get_type not specialized for given type");
    return std::string(); 
}

template <> std::string get_type<X>() {
    return "X"; 
}

int main() {
    get_type<X>();
    get_type<Derived>(); //error 
}

(static_assert - ++ 0x, , BOOST_STATIC_ASSERT. , . , - , X, type_traits.)

+3

, typeid.

+1

, . - ( ).

+1

. X . .

?

+1

class X
{
  static string get_type() {return "X"; }
};

class Y : public X
{
  static string get_type() {return "Y"; }
};

, : get_type . , , , . , . , .

+1

, . , , ( , , ). , , , - .

class Base {
  static std::vector<std::pair<const std::type_info*, int> > datas;
  typedef std::vector<std::pair<const std::type_info*, int> >::iterator iterator;
public:
  virtual ~Base() { }
  int Data() const {
    const std::type_info& info = typeid(*this);
    for(iterator i = datas.begin(); i != datas.end(); ++i)
      if(*(i->first) == info) return i->second;
    throw "Unregistered Type";
  }

  static bool RegisterClass(const Base& p, int data) {
    const std::type_info& info = typeid(p);
    for(iterator i = datas.begin(); i != datas.end(); ++i) {
      if(i->second == data) {
    if(*(i->first) != info) throw "Duplicate Data";
    return true;
      }
      if(*(i->first) == info) throw "Reregistering";
    }
    datas.push_back(std::make_pair(&info, data));
    return true;
  }
};
std::vector<std::pair<const std::type_info*, int> > Base::datas;

class Derived : public Base { };
const DerivedRegisterFlag = Base::RegisterClass(Derived(), 10);

class OtherDerived : public Base { };
const OtherDerivedRegisterFlag = Base::RegisterClass(OtherDerived(), 10); //exception

: . main, . , .

; , type_info::before , , -, , . , type_info. , , typeid, . , , .

. , - .

, , , "" "" . , , , ? , , .

* edit: , , .. , RTTI , , .

+1

Delphi, .; >

+1

, . , , , , static virtual . , , .

/ - -, , , . , . abstract class Theory , , Theory TheoryOfSelf, TheoryOfMind . , express(), , , . , express(). , , static virtual Theory.express() - , ( ) , ( ).

, , static - , , .

, , Theory - , . , , . .

+1

All Articles