I have a Foo and Logger class:
class Logger{};
class Foo{
Foo(Logger& logger);
Logger& logger;
}
Foo::Foo(Logger& logger) : logger(logger)
{}
Now I want to create an array of objects of the Foo class, where all links Foo::loggermust point to the same object Logger. I tried something like (I need both stack distribution and heaps):
Logger log ();
Foo objects [3] (log);
Foo* pObjects = new Foo [3] (log);
The problem is that both versions try to call the default constructor Foo(), which is not there. Also, as I understand it, it is impossible to change the link variable of the link. Therefore, a temporary call to the default constructor and subsequent initialization in a loop also do not help.
So: What is the right way to do this? Do I need to use pointers to an object Logger?
source
share