I have some class Foo and Logger:
class Logger{/* something goes here */};
class Foo{
Foo(Logger& logger);
Logger& logger;
}
Foo::Foo(Logger& logger) : logger(logger)
{}
Now I want to create an array of objects of class Foo, where all references Foo::logger
should point to the same Logger
object. I tried something like (I need both stack and heap allocation):
Logger log (/* parameters */);
Foo objects [3] (log); // On stack
Foo* pObjects = new Foo [3] (log); // On heap
The problem is that both versions try to call the default constructor Foo()
which is not present. Also as I understand it is not possible to change the referenced variable of a reference. So a temporary call to the default constructor and a later initalisation in a loop does also not help.
So: What is the right way to do it? Do I need to use pointers to the Logger
object?