-2

I know that in Java every class extends the Object class which in turn extends the class Class. But how does this work in C++? In there any class equivalent to the class Object or to the class Class in Java?

Thanks.

Daniel Oliveira
  • 1,280
  • 14
  • 36
  • 5
    `Object` doesn't extend `Class`. In fact, `Class` extends `Object` – qxz Nov 20 '16 at 23:19
  • 4
    There is no common root class in C++ – Jim Garrison Nov 20 '16 at 23:19
  • 2
    There isn't a one. C++ is not Java. – Elliott Frisch Nov 20 '16 at 23:19
  • 1
    Why are people downvoting this so much? – qxz Nov 20 '16 at 23:24
  • The closest you could get in C++ would be store `void*` as pointers to your objects. It's useful for a lot of things, but commonly you need a bunch of metadata with it to re-cast it later. (Like Python does) – AndyG Nov 20 '16 at 23:29
  • What would the `Object` be capable of? There's no `hashCode` or `toString` or `getClass` in C++. And no runtime type identification unless you "ask" (and "pay") for it by introducing virtuality. So if the answer is "nothing" then `void*` is good enough for that. – The Vee Nov 21 '16 at 00:04
  • Also: http://stackoverflow.com/questions/11747439/c-object-class I wonder why people desire an `Object` so much. Please use the search for questions which feel so generic that you could hardly imagine being the first one to come up with them. – The Vee Nov 21 '16 at 00:07
  • There's no equivalent but it could easily be done for all *custom* work by simply replicating the Java model in your hierarchy. – ChiefTwoPencils Nov 21 '16 at 02:01

1 Answers1

4

There is no equivalent in C++ for either java.lang.Object or java.lang.Class. Different class hierarchies don't share a common root.

Also, Object does not extend Class. Object has no superclass; An Object is not a type of Class. However, Class does extend Object; an instance of Class, which represents a class, is itself an Object.

qxz
  • 3,814
  • 1
  • 14
  • 29