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 ;)

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

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

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

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.

Wow,I think i made a mistake.
You got it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.