I have two classes, Foo
and Bar
. They both contain a pointer to a Baz
object. I want the Baz
pointers in both an instance of Foo and an Instance of Bar
to point to the same Baz
object.
A Baz
object has a member called name with getter and setter functions.
A Foo
object contains a Bar
object.
A Bar
object has a method to print the name of the Baz
object.
The Baz
objects name must be set in the scope of Foo
.
This is what I have tried:
In the constructor of Foo
, it sets the name of this bazA
, and sets the bazB
belonging to bar
to be equal to the bazA
belonging to Foo
.
//Foo.h
class Foo {
public:
explicit Foo();
Baz* bazA;
Bar bar;
}
//Foo.cpp
Foo::Foo()
{
this->bazA->setName("hello");
this->bar.bazB = bazA;
}
//Bar.h
class Bar{
public:
Baz* bazB;
void printName ();
}
//Bar.cpp
void Bar::printName ()
{
std::cout << "name: " << bazB->getName();
//Should print out "name: hello"
}
When I do this:
//Main.cpp
Foo foo;
foo.bar.printName();
The program crashes because the name member has not been initialised in the Baz
object that belongs to the Bar
object.
But the Baz
object belonging to Bar
should be the same Baz
object that belongs to Foo
, which has had its name set, so it should work.
Why am I getting this error?
How can I correctly share a Baz
object between the Foo
and Bar
classes?