Hello People,
I just came across this code as I was browsing through some C++ problem sets. This code gives compile time error but I am unable to understand the reason. I am C guy and my knowledge about C++ is not that great. So pardon if there is an obvious solution to this problem. Any help would be appreciated

class Base
{
public:
      
     Base();    
     virtual ~Base();
};

class derived: public Base
{
public:                      
       virtual ~derived();
};

  
int main()  
{
    Base *pb = new derived;    
    getch();
}

This code gives compile time error but I am unable to understand the reason.

Which error?
Only thing that I see missing, is a header file for getch(); .

[EDIT]
Oh, and it leaks memory ;)

Edited 6 Years Ago by mitrmkar: n/a

Which error?
Only thing that I see missing, is a header file for getch(); .

Whoops, it is also lacking the implementation of constructor/destructors.

Edited 6 Years Ago by mitrmkar: n/a

Hello People,
I just came across this code as I was browsing through some C++ problem sets. This code gives compile time error but I am unable to understand the reason. I am C guy and my knowledge about C++ is not that great. So pardon if there is an obvious solution to this problem. Any help would be appreciated

class Base
{
public:
      
     Base();    
     virtual ~Base();
};

class derived: public Base
{
public:                      
       virtual ~derived();
};

  
int main()  
{
    Base *pb = new derived;    
    getch();
}

You defined the virtual generator function,I think it is illegal in C++ grammar
The sixth line and twelfth line

Edited 6 Years Ago by sheff_cc: n/a

You defined the virtual generator function,I think it is illegal in C++ grammar
The sixth line and twelfth line

No, it is certainly not so i.e. a virtual destructor is perfectly legal.

This article has been dead for over six months. Start a new discussion instead.