In the following code, I traced by using debugger I fail to understand
1. why B b2() is never called but skipped.
2. Why auto_ptr calls base (A) destructor only when object created is derived (B)?
class A {
public:
A(int x_) : _x(x_) {cout << "A::A( " << _x << ")" << std::endl; }
A(const A& src) : _x(src._x) {cout << "A::A(copy " << _x << ")";}
~A() { std::cout << "A::~A " << std::endl; }
int x() const { return _x; };
protected:
int _x;
};
class B : public A {
public:
B():A(0) {cout << "B::B( " << _x << ")";}
B(A a):A(a.x()) {cout << "B::B(A) ";}
~B() { std::cout << "B::~B "; }
};
int main() {
B b1(11); //which calls A(int) -> B(A a) -> {A::x() -> A(int)} -> ~A()}
B b2(); //It's never called, why?
std::auto_ptr<A> aptr(new B); //Calls A(0)->B()-> ~A() ==> why ~A() only but not ~B() ?
}
/*Actual Result:
B b1(11) => It prints following
A::A(11)
A::A(11)
B::B(A)
A::~A
/// Why B b2() is not called???
auto_ptr<A> aptr(new B) => It prints following. Why ~B() is not called?
A::A(0)
B::B(0)
A::~A
B b1(11) destructors => It prints following
B::~B
A::~A */