Solved

Why virtual desctructor?

Posted on 2013-06-11
2
312 Views
Last Modified: 2013-06-11
class Socket   // This is an Interface class and must be overriden to work.
{
  public:  
    enum SocketErrorValue
    {
      SocketErrorValue_None             = 0,
      SocketErrorValue_SocketNotOpen    = -1,
      SocketErrorValue_ConnectionClosed = -2,
      SocketErrorValue_WriteBufferFull  = -3,
      SocketErrorValue_InvalidParameter = -4,
      SocketErrorValue_Timeout          = -5,
      SocketErrorValue_NoData           = -6,
      SocketErrorValue_InListenMode     = -7
    };


    /// <summary>Descructor
    /// </summary>
    virtual ~Socket() {};
    ...
    ...


In above base class, is destrctor 'virtual' so it can be overridden by a class that inherits this base class?
0
Comment
Question by:naseeam
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
ID: 39238365
The reason for the destructor being virtual is to ensure that the base class' destructors are calledm and that will happen in the right order. See http://www.codersource.net/c/c-miscellaneous/c-virtual-destructors.aspx ("C++ Virtual destructors") and http://blogs.msdn.com/b/oldnewthing/archive/2004/05/07/127826.aspx ("When should your destructor be virtual?")
0
 

Author Closing Comment

by:naseeam
ID: 39238501
Great articles that anwer the exact question asked.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 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

22 Experts available now in Live!

Get 1:1 Help Now