Creating a static class?

C

carl

I have wrapped a simple C++ logger in a class


#include "thirdPartyLogger.h"
class MyLogger {
public:
MyLogger(std::string name, int level){
// Init 3party logger with above parameters.

}


void setup(std::string name, int level) {
// the same as the constructor but necessary for construction.
}

// wrap message functions

};




Now I would like to use this logger in another class:

// .h file
class A {
public:
MyLogger logger; // works fine
MyLogger logger("A",2); // Does NOT work,


}

//.cpp file
A::A(){
logger.setup("name", 2);
}


It is not possible to create the logger using the parameterized constructor
as show above. A solution is to create the setup() function which can be
called in the constructor of A.

But is there no way to create the logger from the parameters but without
using the setup method?


I have some free template functions in a single .h file:

template<typename T>
void fun0(){

}

template<typename T>
void fun1(){

}

Now it could be nice to create the logger like this:

MyLogger logger("Free Functions",3);

and then use the samme logger instance in both the fun0 and fun1 functions.
But this is not possible. I need to call the setup function in both
functions. Any ideas?
 
M

Marcel Müller

carl said:
Now I would like to use this logger in another class:

// .h file
class A {
public:
MyLogger logger("A",2); // Does NOT work,
}

//.cpp file
A::A(){
logger.setup("name", 2);
} [...]
But is there no way to create the logger from the parameters but without
using the setup method?

The right syntax is

A::A()
: logger("A",2)
{}


Marcel
 

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

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top