Connecting Tech Pros Worldwide Help | Site Map

virtual static int xyz; // if bjarne cannot think of it ...

Donald Gillies
Guest
 
Posts: n/a
#1: Oct 3 '05
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;

};

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.

- Don Gillies
San Diego, CA
persenaama
Guest
 
Posts: n/a
#2: Oct 3 '05

re: virtual static int xyz; // if bjarne cannot think of it ...


There's a way.. you can make the baseClass a template, then the "static
ofstream" will be unique instance for each type.

Gianni Mariani
Guest
 
Posts: n/a
#3: Oct 3 '05

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 ?
Dave Rahardja
Guest
 
Posts: n/a
#4: Oct 4 '05

re: virtual static int xyz; // if bjarne cannot think of it ...


On Sun, 02 Oct 2005 22:59:19 -0700, Gianni Mariani <gi2nospam@mariani.ws>
wrote:
[color=blue]
>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]

Am I mistaken, or could the problem be solved by having the logger classes
derive from some common logger class?

class LogFile
{
public:
virtual void Init() = 0;
virtual void Log() = 0;
};

class DefaultLogFile: virtual public LogFile
{
public:
virtual void Init();
virtual void Log();
};

class TypeALogFile: virtual public LogFile
{
public:
virtual void Init();
virtual void Log();
}

class TypeBLogFile: virtual public LogFile
{
public:
virtual void Init();
virtual void Log();
}

class Base
{
public:
void InitLogFile() { GetLogFile().Init(); }
void Log() { GetLogFile().Log(); }
virtual LogFile& GetLogFile();

private:
DefaultLogFile log_file;
};

LogFile& Base::GetLogFile() { return log_file; }

class ChildA
{
public:
virtual LogFile& GetLogFile();
private:
TypeALogFile log_file;
};

LogFile& ChildA::GetLogFile() { return log_file; }

class ChildB
{
public:
virtual LogFile& GetLogFile();
private:
TypeBLogFile log_file;
};

LogFile& ChildB::GetLogFile() { return log_file; }

[color=blue]
>Do you blame Henry for excessive use of oil ?[/color]

;-D

-dr
Closed Thread