Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #9948
| From | "Paul" <pchristor@yahoo.co.uk> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Different ways of representing "address" arguments |
| Date | 2011-09-09 01:20 +0100 |
| Organization | Netfront http://www.netfront.net/ |
| Message-ID | <j4bm57$l1f$1@adenine.netfront.net> (permalink) |
| References | <EpydnR4coofgV_nTnZ2dnUVZ_tednZ2d@westnet.com.au> <967b9c68-65e0-4587-ba69-a296472cd503@u6g2000yqa.googlegroups.com> <e3f0ae81-ed39-402e-a93a-a327b071963c@r17g2000yqa.googlegroups.com> <j46e3v$1r9$1@newsreader4.netcologne.de> |
"Thomas J. Gritzan" <phygon_antispam@gmx.de> wrote in message news:j46e3v$1r9$1@newsreader4.netcologne.de... > Am 06.09.2011 18:04, schrieb Paul: >> On Sep 6, 7:57 am, Goran<goran.pu...@gmail.com> wrote: >>> On Sep 5, 3:11 pm, "Jarrick Chagma" >>> >>> <Jarrick.Cha...@your.mind.gmail.com> wrote: >>>> I am new to C++ and have a basic question. What is the semantic >>>> difference >>>> between these two: >>> >>>> A) void foo(const int&i) >>>> B) void foo(int *i) > > The const in A) applies to the referred to integer. In A) you cannot > change the value, while in B) you can change the pointed to value and the > pointer itself. > > (well, you can only change the local copy of the pointer, but not the > pointer in the calling function.) > >>>> AFAICT, both will result in the address of an integer argument being >>>> passed >>>> to foo() >>>> and in both cases the address cannot be modified. Are there any >>>> differences other than that i will need to be dereferenced in (B)? >>>> Which is >>>> preferred? > > Prefer references unless you need pointer semantics. Prefer const unless > you need to modify. > >>> IMHO, if you see a pointer in a well-designed C++ code, it means >>> there's a program state where said pointer can be null. That also >>> means dealing with said null. >>> >> There is also the case where polymorphic behavour is expected. >> The function may take a Shape pointer parameter type, where a Circle >> or a Polygon is expected. > > Goran wanted to point out a difference between pointers and references. > > A reference cannot (legally) be null like a pointer, but polymorphic > behaviour works equally with references and pointers. > Yes I realise he was pointing out the difference between pointers and references. I was mainly responding to what he said about seeing a pointer in code means there will be a null state someplace, which is actually what I usually think myself. I was looking for a senario where this assumption is not always a prudent assumption. As you correctly pointed out my polymorphism example was not an ideal scenario as I was overlooking the fact that this can also be done using references. Apart from the obvious scenario of re-allocating a different object to the pointer, and the benefits of having a possible null. There is another scenario where it may be beneficial to prefer a pointer over a reference thus it is not always prudent to assume the pointer may be null. That is when the argument is a pointer to an array. --- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
[n00b] Different ways of representing "address" arguments "Jarrick Chagma" <Jarrick.Chagma@your.mind.gmail.com> - 2011-09-05 23:11 +1000
Re: Different ways of representing "address" arguments Saeed Amrollahi <amrollahi.saeed@gmail.com> - 2011-09-05 06:47 -0700
Re: Different ways of representing "address" arguments gwowen <gwowen@gmail.com> - 2011-09-05 06:59 -0700
Re: Different ways of representing "address" arguments Goran <goran.pusic@gmail.com> - 2011-09-05 23:57 -0700
Re: Different ways of representing "address" arguments Paul <pchristor@yahoo.co.uk> - 2011-09-06 09:04 -0700
Re: Different ways of representing "address" arguments "Thomas J. Gritzan" <phygon_antispam@gmx.de> - 2011-09-07 02:32 +0200
Re: Different ways of representing "address" arguments "Paul" <pchristor@yahoo.co.uk> - 2011-09-09 01:20 +0100
Re: Different ways of representing "address" arguments Goran <goran.pusic@gmail.com> - 2011-09-07 00:01 -0700
Re: Different ways of representing "address" arguments Paul <pchristor@yahoo.co.uk> - 2011-09-07 05:09 -0700
Re: [n00b] Different ways of representing "address" arguments Bart van Ingen Schenau <bart@ingen.ddns.info> - 2011-09-06 13:19 +0200
Re: Different ways of representing "address" arguments James Kanze <james.kanze@gmail.com> - 2011-09-06 15:29 -0700
Re: Different ways of representing "address" arguments Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-08 18:53 -0700
csiph-web