Solved

Destructors...!!

Posted on 2004-04-08
5
273 Views
Last Modified: 2010-04-01
Where should I put my delete ??
It doesn't work if I put it in ma base class destructor!

class Personne
{
public:
      Personne(char *s)
      {
            name = new char[strlen(s+1)];
            strcpy(name,s);
      }
      void print()
      {
            cout << "Je m'appelle " << name << endl;
      }
      ~Personne()
      {
                               //PROBLEM
                     delete  []name;
            name = NULL;      
      }

protected:
      char *name;
};

class Etudiant : public Personne
{
public:
      Etudiant(char* s, float m):Personne(s),moyenne(m)
      {
      }
      void print()
      {
            cout << "Je m'appelle " << name <<" et j'ai une moyenne de "
                  << moyenne << endl;
      }
      ~Etudiant()
      {}

private:
      float moyenne;
};

class Prof:public Personne
{
public:
      Prof(char *s,int n):Personne(s),publs(n)
      {
      }
      void print()
      {
            cout << "Je m'appelle " << name << " et j'ai publie "
                  << publs << " ouvrages" << endl;
      }
      
      ~Prof()
      {}
private:
      int publs;

};
0
Comment
Question by:Prog_
5 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 65 total points
ID: 10782769
Make destructor virtual:

virtual ~Personne()
0
 
LVL 14

Assisted Solution

by:wayside
wayside earned 60 total points
ID: 10782779
You have to make the destructor virtual in the base class.
0
 

Author Comment

by:Prog_
ID: 10782780
Sorry...
the problem was strlen(s)+1 and not strlen(s+1)....
it was a typo error...
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10782853
AlexFM,
waysid,

Why does he need to make the base class destructor virtual
This is only required if he has a virtual function in his class

Amit
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10782914
It was just guess (not successful). You need virtual destructor in this case:

Personne* p = new Prof("abc", 1);

delete p;       // Prof destructor is called only if it is virtual
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now