当你有:
B b = a->getB();
一个类型的新对象是从对( )B
的现有实例的引用中创建的。它不是这里调用的,而是复制构造函数。B
B&
B::operator=
每个类都有一个复制构造函数(如果你不显式添加它,编译器会为你提供一个)。它接受一个参数,该参数是对同一类的引用。您没有在上面的代码中放置复制构造函数,所以我假设编译器已经为您生成了一个:
class B
{
public:
B(B& other)
{
// memberwise copy (shallow copy)
};
};
所以A::getB()
返回了对成员的引用,A::b
并且这个引用作为参数传递给B::B(B&)
.
void func()
{
A *a = new A(); // Instance of A is created on the heap;
// (pointer a is a local variable and is on the stack though!)
// A::b is object of type B and it is on the heap as well
B b = a->getB(); // Instance of class B is created on the stack (local variable)
.....
delete a; // deleting A from the heap:
// A::~A is called which calls B::~B (of its member b)
} // a and b go out of the scope; b is an object => B::~B is called