I need some clarification about Structs as Bases of a Derived Object. . The current code I'm maintaining ( Legacy , VS2008 ) had classes with Struct as their bases, and being used like this.
struct Base
{
int nSomeValue;
Base(int m_SomeValue): nSomeValue(m_SomeValue){};
};
class Derived : public Base
{
Derived (int m_OtherValue,int m_SomeOtherValue)
: nOtherValue(m_OtherValue),nSomeOtherValue(m_SomeOtherValue){};
~Derived(){};
public:
int nOtherValue;
int nSomeOtherValue
};
int main()
{
Derived* pDerived = new Derived(10,20);
std::cout<<"Derived Object's Value is " << pDerived->nOtherValue << " and " << pDerived->nSomeOtherValue << std::endl;
delete pDerived;
pDerived = NULL;
}
I noticed that some of the Structs had virtual Destructors in them like this.
struct Base
{
Base(int m_SomeValue): nSomeValue(m_SomeValue){};
virtual ~Base(){};
int nSomeValue;
};
My questions is as follows
#1. Should structs have virtual Destructors let alone a destructor at all?
#2. Is it safe to assume that when a Struct is used as a base without a destructor that the memory allocated for it would be freed when delete is used on the Derived Object?