D
dragoncoder
Consider the following code. Does it produce undefined behaviour
because destructor of A is not virtual ?
#include<iostream>
using namespace std;
class A
{
public:
A()
{
std::cout<<"In A's Constructor"<<std::endl;
}
~A()
{
std::cout<<"In A's Desstructor"<<std::endl;
}
};
class Bublic A
{
public:
B()
{
std::cout<<"In B's Constructor"<<std::endl;
}
virtual ~B()
{
std::cout<<"In B's Desstructor"<<std::endl;
}
};
class Cublic B
{
public:
C()
{
std::cout<<"In C's Constructor"<<std::endl;
}
~C()
{
std::cout<<"In C's Desstructor"<<std::endl;
}
};
int main()
{
A *b = new C();
delete b;
return 0;
}
because destructor of A is not virtual ?
#include<iostream>
using namespace std;
class A
{
public:
A()
{
std::cout<<"In A's Constructor"<<std::endl;
}
~A()
{
std::cout<<"In A's Desstructor"<<std::endl;
}
};
class Bublic A
{
public:
B()
{
std::cout<<"In B's Constructor"<<std::endl;
}
virtual ~B()
{
std::cout<<"In B's Desstructor"<<std::endl;
}
};
class Cublic B
{
public:
C()
{
std::cout<<"In C's Constructor"<<std::endl;
}
~C()
{
std::cout<<"In C's Desstructor"<<std::endl;
}
};
int main()
{
A *b = new C();
delete b;
return 0;
}