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)
Path: q11ni107278614wiw.1!nntp.google.com!feed-C.news.volia.net!volia.net!news2.volia.net!feed-A.news.volia.net!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!news-feed.eu.lambdanet.net!news.bcc.de!newsfeeder.ewetel.de!82.197.223.103.MISMATCH!feeder3.cambriumusenet.nl!feeder1.cambriumusenet.nl!feed.tweaknews.nl!81.171.88.16.MISMATCH!eweka.nl!hq-usenetpeers.eweka.nl!newsfeed101.telia.com!starscream.dk.telia.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail
From: Keith Thompson <ks...@mib.org>
Newsgroups: alt.comp.lang.learn.c-c++
Subject: Re: 'Overloading' Class member functions and Virtual functions
Date: Tue, 07 Aug 2012 17:47:18 -0700
Organization: None to speak of
Lines: 50
Message-ID: <lnk3xaw6p5.fsf@nuthaus.mib.org>
References: <aPbUr.809012$Hs2.327416@fx27.am4> <jvs6b7$tj8$1@dont-email.me>
Mime-Version: 1.0
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)
Cancel-Lock: sha1:ud1sMIuZVDsCFGB/DpfQePRMGRM=
sha1:w9Ft6COEO4AY+ojw4fhzS1Ta+J4=
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
parent *ptr;
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
ptr->func();
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"