C
chris
Hi all,
I need to pass reference to a class via constructor,
to initialize its member variable. This is the code simplified :
class Logger{
public :
Logger(string filename);
};
class Destination{
public :
Destination(Logger &log)
private:
Logger mylogger;
};
int main(int argc, char* argv[]){
Logger logger("logger.config");
Destination dest(logger);
}
In this case, Logger class doesn't have no-args constructor.
How can I pass (by reference ) Logger instance I create on
main function? By now, I do it by defining member variable
mylogger as reference, such this :
private :
Logger& mylogger
and I initialize it using initialization list in constructor
Destination:estination(Logger& log):mylogger(log){}
Is the way I did it correct ? Or any side effect that I have to
take care ?
Thanks in advance.
I need to pass reference to a class via constructor,
to initialize its member variable. This is the code simplified :
class Logger{
public :
Logger(string filename);
};
class Destination{
public :
Destination(Logger &log)
private:
Logger mylogger;
};
int main(int argc, char* argv[]){
Logger logger("logger.config");
Destination dest(logger);
}
In this case, Logger class doesn't have no-args constructor.
How can I pass (by reference ) Logger instance I create on
main function? By now, I do it by defining member variable
mylogger as reference, such this :
private :
Logger& mylogger
and I initialize it using initialization list in constructor
Destination:estination(Logger& log):mylogger(log){}
Is the way I did it correct ? Or any side effect that I have to
take care ?
Thanks in advance.