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

D

Donald Gillies

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
 
P

persenaama

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

Gianni Mariani

Donald said:
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;

};

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.
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.

Do you blame Henry for excessive use of oil ?
 
D

Dave Rahardja

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.

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; }

Do you blame Henry for excessive use of oil ?

;-D

-dr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top