0

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();
}
3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by sheff_cc
0

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 by mitrmkar: n/a

0

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 by mitrmkar: n/a

0

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 by sheff_cc: n/a

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.