| re: virtual static int xyz; // if bjarne cannot think of it ...
Donald Gillies wrote:[color=blue]
> So I have a virtual base class. I want each child class to have its
> own unique log. There may be hundreds of child classes but all
> classes of a particular type should share one log.
>
> After scratching my head a few moments I decided what I really wanted
> was this :
>
> class baseClass {
>
> virtual static ofstream _logFile;
>
> void InitLogFile(char *baseName);
> void LogSomething(int a, int b, ...);
>
> };
>
> class Child : public baseClass {
>
> static ofstream _logFile;
>
> };[/color]
template <typename T>
class Logger
{
public :
static ofstream m_logFile;
..... Init... Log etc
};
class Child
: public Logger<Child>
{
.... stuff
};
This is called the Curiously Recurring Template Pattern where a class is
derived from a templated base class that depends on the derived class.
In this case you have no need for virtual methods. Some people also
refer to this as template based polymorphism.
[color=blue]
>
> There really isn't a way to do this well in C++, is there? As someone
> who knows a little about C++ and assembly language, it would not be
> hard to make the virtual storage a pointer in vtable that points to
> the static storage - different for every subclass.
>
> I cannot think of a way of doing it without burning a lot of needless
> storage. I think this supports my belief that if Bjarne couldn't
> think of it in 1986, your application of C++ was NOT PROVIDED FOR in
> the design of the C++ programming language ... EOT.[/color]
Do you blame Henry for excessive use of oil ? |