I have problem with initializing pointer to Bird in BirdHouse's constructor initialization list. It seems that it doesn't point to the object that I want to point to.
Here is the Bird class:
class Bird
{
std::string name;
static int objectCount;
public:
Bird () : name("Bird #" + std::to_string(objectCount))
{
++objectCount;
}
Bird (const Bird& bb) : name(bb.name + " copy")
{
}
friend class BirdHouse;
};
And here is the BirdHouse class:
class BirdHouse
{
Bird b;
Bird * bp;
Bird & br;
public:
BirdHouse(Bird aa, Bird bb, Bird cc) : b(aa), bp(&bb), br(cc) {}
void print() const
{
std::cout << "Bird b = " << b.name << std::endl;
std::cout << "Bird * bp = " << bp->name << std::endl;
std::cout << "Bird & br = " << br.name << std::endl;
}
};
When I want to call BirdHouse::print() in main() the program crashes:
int main()
{
Bird b1;
Bird b2;
Bird b3 = b2;
BirdHouse bh1(b1,b2,b3);
// bh1.print(); // I can't call it because b2 points to weird stuff
return 0;
}
How should I initialize BirdHouse object so it points to appropriate Bird object?
The crash is because of undefined behavior, because you save a pointer to an argument that is passed by value, and that object will be destructed once the function returns.
There are two possible solutions as I can see it: Either pass the variable as a pointer to begin with, or pass it by reference. However, be careful because if the lifetime of the BirdHouse
object is longer than the lifetime of the object you pass in (as a pointer or a reference) you will once again have undefined behavior.