www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/04/09:58:13

Message-ID: <359D624A.3C0599DA@uol.com.br>
Date: Fri, 03 Jul 1998 19:59:22 -0300
From: "Juciê Dias Andrade" <jucie AT uol DOT com DOT br>
MIME-Version: 1.0
To: A White <awhite AT sympatico DOT ca>
CC: djgpp AT delorie DOT com
Subject: Re: g++: inheriting static members
References: <359CFE52 DOT 2A0B AT sympatico DOT ca>

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019