linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Virtual functions and destructors
@ 2006-03-19 15:20 Shriramana Sharma
  2006-03-19 17:38 ` Steve Graegert
  0 siblings, 1 reply; 6+ messages in thread
From: Shriramana Sharma @ 2006-03-19 15:20 UTC (permalink / raw)
  To: Linux C Programming List

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

Please elucidate. Thank you.

-- 

Tux #395953 resides at http://samvit.org
playing with KDE 3.51 on SUSE Linux 10.0
$ date [] CCE +2006-03-19 W11-7 UTC+0530

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Virtual functions and destructors
  2006-03-19 15:20 Virtual functions and destructors Shriramana Sharma
@ 2006-03-19 17:38 ` Steve Graegert
  2006-03-22 22:27   ` Benjamin Sobotta
  0 siblings, 1 reply; 6+ messages in thread
From: Steve Graegert @ 2006-03-19 17:38 UTC (permalink / raw)
  To: linux-c-programming

On 3/19/06, Shriramana Sharma <samjnaa@gmail.com> 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Virtual functions and destructors
  2006-03-19 17:38 ` Steve Graegert
@ 2006-03-22 22:27   ` Benjamin Sobotta
  2006-03-23  2:02     ` Shriramana Sharma
  0 siblings, 1 reply; 6+ messages in thread
From: Benjamin Sobotta @ 2006-03-22 22:27 UTC (permalink / raw)
  To: Steve Graegert; +Cc: linux-c-programming

On Sunday 19 March 2006 18:38, Steve Graegert wrote:
> On 3/19/06, Shriramana Sharma <samjnaa@gmail.com> 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_destructo
> >r.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
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

Hello!

to #1:
I'm not sure if this is right. Functions can always be overloaded. virtual 
merely indicates that you want to use dynamic binding. That means that it is 
determined at run time rather than compile time which function to run. The 
decision will be made based on the type of the actual object rather than the 
pointer. If you remove both virtual statements parent will show up twice. 
That's because in both cases the pointer is of type parent. In case of 
dynamic binding it doesn't matter anylonger, because the function belonging 
to the object is executed. 
Further I believe that once a function is declared virtual it always stays 
that way. Even if in subsequently derived classes it is not explicitly 
declared virtual any longer. 

Cheers,

Ben

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Virtual functions and destructors
  2006-03-22 22:27   ` Benjamin Sobotta
@ 2006-03-23  2:02     ` Shriramana Sharma
  2006-03-23 22:00       ` Benjamin Sobotta
  0 siblings, 1 reply; 6+ messages in thread
From: Shriramana Sharma @ 2006-03-23  2:02 UTC (permalink / raw)
  To: Linux C Programming List

Thursday, 23 March 2006 03:57 samaye tvayaa likhitam:

> I'm not sure if this is right. Functions can always be overloaded. 

He did not speak of functions being overloaded. He spoke of functions being 
over-*ridden*.

> Further I believe that once a function is declared virtual it always stays
> that way. Even if in subsequently derived classes it is not explicitly
> declared virtual any longer.

Verified this using a grandchild class.

-- 

Tux #395953 resides at http://samvit.org
playing with KDE 3.51 on SUSE Linux 10.0
$ date [] CCE +2006-03-23 W12-4 UTC+0530

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Virtual functions and destructors
  2006-03-23  2:02     ` Shriramana Sharma
@ 2006-03-23 22:00       ` Benjamin Sobotta
  2006-03-24  1:07         ` Glynn Clements
  0 siblings, 1 reply; 6+ messages in thread
From: Benjamin Sobotta @ 2006-03-23 22:00 UTC (permalink / raw)
  To: Shriramana Sharma; +Cc: Linux C Programming List

On Thursday 23 March 2006 03:02, Shriramana Sharma wrote:
> Thursday, 23 March 2006 03:57 samaye tvayaa likhitam:
> > I'm not sure if this is right. Functions can always be overloaded.
>
> He did not speak of functions being overloaded. He spoke of functions being
> over-*ridden*.

Sorry I must have read wrong. Nevertheless even if you prefer to say override 
you can still override a function without using virtual. Just adjust the type 
of the pointer to the class of which you want to execute the member function.

Ben

>
> > Further I believe that once a function is declared virtual it always
> > stays that way. Even if in subsequently derived classes it is not
> > explicitly declared virtual any longer.
>
> Verified this using a grandchild class.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Virtual functions and destructors
  2006-03-23 22:00       ` Benjamin Sobotta
@ 2006-03-24  1:07         ` Glynn Clements
  0 siblings, 0 replies; 6+ messages in thread
From: Glynn Clements @ 2006-03-24  1:07 UTC (permalink / raw)
  To: Benjamin Sobotta; +Cc: Shriramana Sharma, Linux C Programming List


Benjamin Sobotta wrote:

> > > I'm not sure if this is right. Functions can always be overloaded.
> >
> > He did not speak of functions being overloaded. He spoke of functions being
> > over-*ridden*.
> 
> Sorry I must have read wrong. Nevertheless even if you prefer to say override 
> you can still override a function without using virtual. Just adjust the type 
> of the pointer to the class of which you want to execute the member function.

That isn't really "overriding" anything, just calling a different
function.

Two functions (members or not) which have the same name but different
argument types are completely separate functions. The fact that they
have the same name is (almost[1]) entirely cosmetic; you could just as
easily give them separate names. E.g. if you have:

	void foo(int);
	void foo(float);

you could equally just use:

	void foo_i(int);
	void foo_f(float);

Overloading just lets you omit the type annotation from the name.

[1] The only situation where the ability to overload function names
really makes a difference is when using templates.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-03-24  1:07 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-03-19 15:20 Virtual functions and destructors Shriramana Sharma
2006-03-19 17:38 ` Steve Graegert
2006-03-22 22:27   ` Benjamin Sobotta
2006-03-23  2:02     ` Shriramana Sharma
2006-03-23 22:00       ` Benjamin Sobotta
2006-03-24  1:07         ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).