Message from discussion 'Overloading' Class member functions and Virtual functions
Received: by 10.180.24.165 with SMTP id v5mr1202577wif.1.1344851148842;
Mon, 13 Aug 2012 02:45:48 -0700 (PDT)
From: Keith Thompson <ks...@mib.org>
Subject: Re: 'Overloading' Class member functions and Virtual functions
Date: Tue, 07 Aug 2012 17:47:18 -0700
Organization: None to speak of
References: <aPbUr.809012$Hs2.email@example.com> <firstname.lastname@example.org>
Injection-Info: mx04.eternal-september.org; posting-host="5842a546a2d4559ed11e9ac4721da702";
logging-data="4328"; mail-complaints-to="ab...@eternal-september.org"; posting-account="U2FsdGVkX18XyhbRYXkR5eBJi+CM+lcL"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)
Content-Type: text/plain; charset=us-ascii
Bill Gill <billne...@cox.net> writes:
> Overloading is a very useful concept. Basically you can write
> several functions and then use the same function name for a number of
> different types. Ben Becarisse mentioned the += function. That is
> obviously a good example of a very useful overload.
Remember that both operators and functions can be overloaded.
Overloading is handy for things like, say, "insert", which could be
applied to any of a number of different data structures.
> Virtual functions are very useful because you can use the function
> in the children of a parent class while using the same function name,
> even though it might be implemented slightly differently. So to
> perform the same operation on a different child you call the same
> function. It saves a lot of confusion over having each child use
> a differently named function for the same type of operation on
> different children.
That's correct, but it doesn't really distinguish it from overloading.
They're both ways of having two or more functions with the same name,
but there are significant differences.
For a call to an overloaded function, it's always possible to determine
at compile time which function will be called. In essence, they're
syntactic sugar; you could eliminate overloading from a program by
giving all overloaded functions unique names.
On the other hand, if you have a pointer object like
then it could, at run time, point either to an object of the parent
class *or* to an object of a child class. If you then call
and func() is virtual, then the determination of which function to call
is made at run time.
> If I didn't get that quite right keep in mind that I am still learning
> this and forgive me. Hopefully somebody will correct me.
More of an expansion than a correction.
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"