Mail Archives: djgpp/1998/07/04/09:58:13
There is another way to achieve the same efect.
You can declare a virtual funtion in the basic class, thus "overriding"
what would otherwise be a static data member.
The trick:
class Base
{
public:
virtual const char *className() const { return "Base"; }
};
class Derived : public Base
{
public:
virtual const char *className() const { return "Derived"; }
};
class AnotherDerived : public Base
{
public:
virtual const char *className() const { return "AnotherDerived"; }
};
#include <stdio.h>
void showClassName(const Base *object)
{
puts(object->className());
}
int main()
{
showClassName(new Base);
showClassName(new Derived);
showClassName(new AnotherDerived);
return 0;
}
Did you understand?
[]s
Obs.: Excuse my poor English, that's not my native language (need I a
compiler?).
A White escreveu:
> I'm playing with a small bit of code and wanted to do the following:
>
> class obj {
> public:
> static const char Name[];
> };
>
> class newbie: public obj
> {
> ...
> };
>
> class doobie: public obj
> {
> ...
> };
>
> I'd want to initialize the obj::Name to "SimpleObj", and the other two to
> "newbie" and "doobie" respecitvely.
- Raw text -