A
Alden Pierre
Hello,
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7
As per the link above it's wise to have a virtual deconstructor when
creating an abstract class. Here is when I'm little confused. Am I
deleting the right object when I call the virtual deconstructor? I was
under impression when creating a class if I'm not specifically
allocating memory, do not implement deconstructor and let the default
take care of it for you. I have the following code:
-------------------------------------------------------------------------
File: person.h
-------------------------------------------------------------------------
#ifndef _PERSON_H
#define _PERSON_H
#include <iostream>
using std::string;
class Person
{
public:
Person( string, string );
void setName( string );
void setSSN( string );
const string getName( void ) const {return name;}
const string getSSN( void ) const {return ssn;}
virtual void print( void ) = 0;
virtual ~Person();
private:
string name;
string ssn;
};
#endif
-----------------------------------------------------------------------------------------
File: person.cpp
-----------------------------------------------------------------------------------------
#include "person.h"
using std::cout;
using std::endl;
Person:erson( string in_name, string in_ssn )
{
setName( in_name );
setSSN( in_ssn );
}
Person::~Person()
{
delete this; // would this delete any derived class? return;
}
void Person::setName( string in_name )
{
name =in_name;
}
void Person::setSSN( string in_ssn )
{
ssn =in_ssn;
}
void Person:rint( void )
{
cout << "Name: " << name << endl;
cout << "SSN: " << ssn << endl;
}
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7
As per the link above it's wise to have a virtual deconstructor when
creating an abstract class. Here is when I'm little confused. Am I
deleting the right object when I call the virtual deconstructor? I was
under impression when creating a class if I'm not specifically
allocating memory, do not implement deconstructor and let the default
take care of it for you. I have the following code:
-------------------------------------------------------------------------
File: person.h
-------------------------------------------------------------------------
#ifndef _PERSON_H
#define _PERSON_H
#include <iostream>
using std::string;
class Person
{
public:
Person( string, string );
void setName( string );
void setSSN( string );
const string getName( void ) const {return name;}
const string getSSN( void ) const {return ssn;}
virtual void print( void ) = 0;
virtual ~Person();
private:
string name;
string ssn;
};
#endif
-----------------------------------------------------------------------------------------
File: person.cpp
-----------------------------------------------------------------------------------------
#include "person.h"
using std::cout;
using std::endl;
Person:erson( string in_name, string in_ssn )
{
setName( in_name );
setSSN( in_ssn );
}
Person::~Person()
{
delete this; // would this delete any derived class? return;
}
void Person::setName( string in_name )
{
name =in_name;
}
void Person::setSSN( string in_ssn )
{
ssn =in_ssn;
}
void Person:rint( void )
{
cout << "Name: " << name << endl;
cout << "SSN: " << ssn << endl;
}