I have a class called Foo that has another class called Bar as a field. Bar only has a constructor with arguments. How do I choose the constructor with arguments for Bar (without storing a Bar* instead of Bar, and calling new)?
Foo.h
class Foo
{
private:
Bar bar;
};
Bar.h
class Bar
{
public:
Bar(int arbitraryArg);
};
Foo.cpp
Foo::Foo()
: bar(bar(1))
{}
I want to do the above because I hear that it is generally not good to store pointers as fields unless absolutely necessary to avoid managing memory, so I want to avoid something like this:
Foo.h
class Foo
{
private:
Bar* bar;
public:
Foo();
}
Foo.cpp
Foo::Foo()
: bar(NULL)
{
bar = new Bar(1);
}