Re: Virtual functions and destructors

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 3/19/06, Shriramana Sharma <samjnaa@xxxxxxxxx> wrote:
> I did not get what is intended to be conveyed by:
>
> http://charm.cs.uiuc.edu/users/olawlor/ref/examples/cpp/virtual.cpp
> http://charm.cs.uiuc.edu/users/olawlor/ref/examples/cpp/virtual_destructor.cpp

1. In C++ the virtual keyword identifies to the compiler that a class
function may be overridden in a derived class.  That is, the derived
class may supply a function of the same name and argument list, but
with different functionality defined in the function body.  In the
example you linked to the child class has overriden the virtual
function hello and allows itself to be overridden by declaring its
function virtual as well.  A pointer to a derived class object may be
assigned to a base class pointer, and a virtual function called
through the pointer. If the function is virtual and occurs both in the
base class and in derived classes, then the right function will be
picked up based on what the base class pointer "really" points at.

2. Virtual destructors are needed to allow for the destructors for the
actual object types derived from the base class to be called.  Non
virtual destructors in base classes can result in memory leaks.

	\Steve
-
: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux