I have a class set up like this:
class Foo {
Foo();
private:
Bar m_bar;
}
That is the class definition in it’s own header file, and now in the source file to go with it I have the constructor and I tried doing this:
Foo::Foo() {
m_bar("parameters for the Bar constructor");
}
However this doesn’t work and gives me an error. I can make m_bar a pointer and then in Foo’s constructor do this:
m_bar = new Bar("parameters here");
However that makes m_bar a pointer and I don’t want that.
I’m not the best with C++ classes and pointers, so could something either explain a way for me to have m_bar defined in the Foo class but constructor somewhere else or if it is better to make m_bar a pointer in this situation explain why? While I would rather not make it a pointer(because I don’t understand pointers extremely well), if that is the best way to do it then I would rather do it that way, but I’d still like someone to explain why that is the best way to do it(if it is).
Yes, using the initializer list syntax: