
Originally Posted by
sunil.thaha
Y have
Please, don't use such ugly abbreviations.

Originally Posted by
sunil.thaha
... declared className() as a member function rather than a static function ?
Because it wouldn't make sense. You need className() when you don't know the class name.
If className() was static, you would have to use it like this:
QString name
( SomeClass
::className() );
QString name( SomeClass::className() );
To copy to clipboard, switch view to plain text mode
Or you can write simply:
QString name( "SomeClass" );
To copy to clipboard, switch view to plain text mode
The effect is the same and in either case you will have to change your code when you rename your class. The only difference, is that in former case you won't make a typo.
Now consider this:
BaseClass *base = new SomeClassDerivedFromBase();
qDebug() << base->className();
BaseClass *base = new SomeClassDerivedFromBase();
qDebug() << base->className();
To copy to clipboard, switch view to plain text mode
With inheritance mechanism, you can have a pointer that points to an instance of some class derived from a base class, but you not necessarily have to know which one and that's when you have to invoke className().
Bookmarks