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?