Skip to main content
GameDev.net gamedev.net
🔒 Locked

default inheritance - like Java Object?

Started by FxMazter Apr 30, 2004 at 3:20 PM 1 replies 250+ views
Original Post
FxMazter
FxMazter
Hello! I was wondering if there is any way to "force" every class in c++ to inherit from some special class by your choice? Just like Java forces each class to inherit from the Object class by default... Maybe with macros or something? c ya
antareus
antareus
No, and with good reason -- overly abstract base classes are not in the spirit of C++. I see little real benefit that a generic Object class can provide C++.

--
OpenIM 0.10 - a native, component-based GPL Win32 instant messaging client
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Matei
Matei
The only reason why you might need an Object superclass for all objects is to create, for example, a collection that stores objects of different types and therefore is declared to store variables of the superclass Object, but in C++, templates allow you to create a version of each container class for each type of contents. The other reason for that in Java was because there were some methods that had to be defined by each object for the API to work, things like toString() and hashCode(); but there are no such restrictions in C++, and if you need them, you would have to create a whole object hierarchy yourself anyway so you might as well make them all inherit from Object.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.