Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #9857

Re: Different ways of representing "address" arguments

From "Thomas J. Gritzan" <phygon_antispam@gmx.de>
Newsgroups comp.lang.c++
Subject Re: Different ways of representing "address" arguments
Date 2011-09-07 02:32 +0200
Organization news.netcologne.de
Message-ID <j46e3v$1r9$1@newsreader4.netcologne.de> (permalink)
References <EpydnR4coofgV_nTnZ2dnUVZ_tednZ2d@westnet.com.au> <967b9c68-65e0-4587-ba69-a296472cd503@u6g2000yqa.googlegroups.com> <e3f0ae81-ed39-402e-a93a-a327b071963c@r17g2000yqa.googlegroups.com>

Show all headers | View raw


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.

-- 
Thomas

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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