base destructor called twice after derived object? - c++

hey there, why is the base destructor called twice at the end of this program?
#include <iostream>
using namespace std;
class B{
public:
B(){
cout << "BC" << endl; x = 0;
}
virtual ~B(){
cout << "BD" << endl;
}
void f(){
cout << "BF" << endl;
}
virtual void g(){
cout << "BG" << endl;
}
private:
int x;
};
class D: public B{
public:
D(){
cout << "dc" << endl; y = 0;
}
virtual ~D(){
cout << "dd" << endl;
}
void f(){
cout << "df" << endl;
}
virtual void g(){
cout << "dg" << endl;
}
private:
int y;
};
int main(){
B b, * bp = &b;
D d, * dp = &d;
bp->f();
bp->g();
bp = dp;
bp->f();
bp->g();
}

Destructors are called in order, as if they were unwinding the effects of the corresponding constructors. So, first the destructors of the derived objects, then the destructors of the base objects. And making the destructors virtual doesn't have any impact on calling / not calling the base class destructor.
Also to mention, your example could be simplified this way (this code also results in calling the base destructor twice and the derived destructor once):
struct A {
~A() {
// ...
}
};
struct B: A {
~B() {
// ...
}
};
int main() {
A a;
B b;
}

Once it is called for b and once for d
Note when destructor of D called it is automatically calls the destructor of B it is different from ordinary virtual functions. where you need explicitly call base class function to use it.

Related

Why static_cast make the destructor called

#include <iostream>
using namespace std;
#define debug(x) cout << #x << ": " << (x) << endl;
class Base {
public:
void f() { cout << "Base:f()" << endl; }
~Base() { cout << "Base:~Base()" << endl; }
};
class A : public Base {
public:
~A() { cout << "A:~A()" << endl; }
};
int main() {
{
A a;
static_cast<Base>(a);
}
return 0;
}
In this code
I call the static_cast to covert the A to Base,
the output tells me that it calls the ~Base() twice?
why the static_cast make the destructor be called?
static_cast<Base>(a); constructs a temporary Base, which is copied from a. After the full expression the temporary gets destroyed, the destructor of Base is called.
After that, when get out of the scope a gets destroyed, the destructor of A and Base are called in order. Note that this has nothing to do with the static_cast.

Creating base class object with a derived class

I have the following code:
class A {
public:
A() { cout << "A()" << endl; }
void f() { cout << "A" << endl; }
};
class B : public A {
public:
B() { cout << "B()" << endl; }
void f() { cout << "B" << endl; }
void ff() { cout << "ff()" << endl; }
};
int main()
{
A a = B();
B b;
a = b;
}
How calling A a = B(); will be different from A a = A();? Why is the conversion from derived class to base class possible?
When you do A a = B();, copy constructor of class A should be invoked after default constructor of class B has been invoked (Note: Compiler may remove unnecessary copy using copy elison).
A(const A& other)
{
}
Since, object of class B can be passed to copy constructor of class A, it will be allowed but you may experience object-slicing.
If you are curious to know "how is it possible to pass a derived class object to a method accepting a reference to the base class", read the following:
Is it possible to pass derived classes by reference to a function taking base class as a parameter

Access overriden parent virtual method in C++

In the following code, how can I access Base::g() from pBase? (and still get "pBase->g();" to work as it does below)
#include <iostream>
using namespace std;
class Base
{
public:
virtual void f(){ cout << "Base::f()" << endl; }
virtual void g(){ cout << "Base::g()" << endl; }
void h(){ cout << "Base::h()" << endl; }
};
class Derived : public Base
{
public:
void f(){ cout << "Derived::f()" << endl; }
virtual void g(){ cout << "Derived::g()" << endl; }
void h(){ cout << "Derived::h()" << endl; }
};
int main()
{
Base *pBase = new Derived;
pBase->f();
pBase->g();
pBase->h();
Derived *pDerived = new Derived;
pDerived->f();
pDerived->g();
pDerived->h();
return 0;
}
Output is:
Derived::f()
Derived::g()
Base::h()
Derived::f()
Derived::g()
Derived::h()
Also, is Derived::f() exactly the same as Derived::g()? (ie. automatically defined as virtual?)
Use pBase->Base::g(); to force the call of g in Base.
Yes, Derived::f is virtual. I personally find the re-emphasising of virtual to be in poor taste. Since C++11, you can use the override specifier on overridden functions, and then a compiler issues a diagnostic if virtual is dropped from the base class.

Why is this the output of this program?

#include <iostream>
using namespace std;
class A
{
public:
A()
{
cout << "A ctor" << endl;
}
virtual ~A()
{
cout << "A dtor" << endl;
}
virtual void foo() = 0;
};
class B : public A
{
public:
B()
{
cout << "B ctor" << endl;
}
virtual ~B()
{
cout << "B dtor" << endl;
}
virtual void foo()
{
cout <<"B's foo" << endl;
}
};
class C : public A
{
public:
C() {
cout << "C ctor" << endl;
}
virtual ~C()
{
cout << "C dtor" << endl;
}
virtual void foo() {cout << "C's foo" << endl;
}
};
int main ()
{
C *ptr = new C[1];
B b;
return 0;
}
This gives the following output:
A ctor
C ctor
A ctor
B ctor
B dtor
A dtor
I don't understand why this is happening. For example, I know that a new C object is being created, that's derived from A, so the A ctor runs first. Then the C ctor runs. And then I thought the C dtor runs, but for some reason the A ctor is running again.
C is created, this constructs A (base class) and then C
B is created, this constructs A (base class) and then B
B is destroyed (goes out of scope), this destructs B and then A (base class)
C is never deleted, so it's leaked and the destructors are never called.

sending an object of son instead of the father

I have a function that gets an object of 'A', it's called: func. I send it an object of B.
Shouldn't it activate the constructor of B? cause I send an object of B although I have to send an object of A. I really don't know why this function prints:
A()
DESTRUCTOR A
and not:
CONSTRUCTOR A
CONSTRUCTOR B
B()
DESTRUCTOR B
DESTRUCTOR A
doesn't it have to multiply the object B because the func gets A a and not A& a?
this is my code:
class A
{
public:
A() { cout << "CONSTRUCTOR A\n"; }
~A() { cout << "DESTRUCTOR A\n"; }
virtual void f() { cout << "A()" << endl; }
};
class B: public A
{
public:
B() { cout << "CONSTRUCTOR B\n"; }
~B() { cout << "DESTRUCTOR B\n"; }
virtual void f() { cout << "B()" << endl; }
};
void func(A a) {
a.f();
}
int main() {
B b;
func(b);
return 0;
}
Because if you send son but the function asks for the father class it treats the child as a father.
class A
{
public:
A() { cout << "CONSTRUCTOR A\n"; }
~A() { cout << "DESTRUCTOR A\n"; }
virtual void f() { cout << "A()" << endl; }
};
class B: public A
{
public:
B() { cout << "CONSTRUCTOR B\n"; }
~B() { cout << "DESTRUCTOR B\n"; }
virtual void f() { cout << "B()" << endl; }
};
void func(B a) {
a.f();
}
int main() {
B b;
func(b);
return 0;
}
if you try this code you see the result you expect.
I think the reason that you are confused is because you are passing by value instead of a pointer. That is why the B object is being treated as an A, since func takes a value of A it treats the object as A by default, which also means that the wrong destructor is being called.Run this code and you will understand better.
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "CONSTRUCTOR A\n"; }
~A() { cout << "DESTRUCTOR A\n"; }
virtual void f() { cout << "A()" << endl; }
};
class B: public A
{
public:
B() { cout << "CONSTRUCTOR B\n"; }
~B() { cout << "DESTRUCTOR B\n"; }
virtual void f() { cout << "B()" << endl; }
};
void func(A * a) {
a->f();
}
int main() {
B * b = new B();;
cout << "\n";
func(b);
cout << "\n";
delete b;
return 0;
}
This is basically the point of virtual functions, when using inheritance you want to able able to pass everything in as a parent class pointer. However the method you want called is the child classes method. When you pass by value the object has to be treated as what it is passed in as.