Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Destructors...!!

Posted on 2004-04-08
5
Medium Priority
?
280 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 260 total points
ID: 10782769
Make destructor virtual:

virtual ~Personne()
0
 
LVL 14

Assisted Solution

by:wayside
wayside earned 240 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

926 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