tags:

views:

195

answers:

2

Hi All,

Normally most of the MFC (window based classes) are derived from CObject. What does CObject do? Why some MFC classes are not required to be derived from CObject?

+3  A: 

According to MSDN CObject mainly provides these features:

  • Serialization support
  • Run-time class information
  • Object diagnostic output
  • Compatibility with collection classes

So when none of this is needed, there is no need to derive from CObject.
There a bit of overhead (DECLARE/IMPLEMENENT_SERIAL/DYNAMIC macros) involved when deriving from CObject, too, so there may be simpler ways of doing things than by deriving from CObject.

mxp
+2  A: 

The MFC documentation covers this pretty well. The documentation for CObject describes what it does (serialization support, runtime class information etc).

The Hierarchy Chart is a good overview, and shows which classes aren't derived from CObject.

You can conclude that the classes that don't derive from CObject are the ones that don't need the services it provides. The reasons are various: for example a class like CFileTime is a simple data type.

Joe