Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #9798
| From | Bart van Ingen Schenau <bart@ingen.ddns.info> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: [n00b] Different ways of representing "address" arguments |
| Date | 2011-09-06 13:19 +0200 |
| Organization | No organisation in particular |
| Message-ID | <j44vjv$6pp$1@localhost.localdomain> (permalink) |
| References | <EpydnR4coofgV_nTnZ2dnUVZ_tednZ2d@westnet.com.au> |
Jarrick Chagma 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) > > AFAICT, both will result in the address of an integer argument being > passed to foo() and in both cases the address cannot be modified. This is not neccesarilly true. In version A, the name 'i' inside foo is an alias to the argument passed to foo in the call. The most common way for a compiler to implement these semantics is by actually passing the address of the argument, but there is no requirement to do it that way. > Are there any differences other than that i will need to be > dereferenced in (B)? Which is preferred? One major difference between A and B, as they are declared now, is that in A the object that 'i' refers to can not be changed from within foo, while in B the object that 'i' points at *can* be changed. For indicating which is preferred, lets look at all the ways you can pass arguments: - By value: void foo(int i): This is preferred when passing (small) objects/values to a function, where the function is not expected to change the passed object. - By const reference: void foo(const int& i): This is preferred when passing large(-ish) objects/values to a function, where the function is not supposed to change the passed object. - By pointer: void foo(const int* i) or void foo(int* i): This is preferred when you need a mechanism to indicate the absence of a value. Use the first variant if the function is not supposed to update the argument. - By non-const reference: void foo(int& i): This is preferred when foo is supposed to update the argument and the argument is mandatory. > > -- > Regards, > > Jarrick > Bart v Ingen Schenau
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