Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > constant references

Reply
Thread Tools

constant references

 
 
Leslaw Bieniasz
Guest
Posts: n/a
 
      03-13-2008

Cracow, 13.03.2008

Hi,

Assuming that I want to pass a reference to a function, and
make sure that the function will not make any changes to the object
passed, is it necessary to declare two "const's" or only one, i.e.
for example:

void fun(const Type& const x);

or

void fun(const Type& x);

Also, is there a difference between

const Type& const x

and

const Type const & x


L. Bieniasz



 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      03-13-2008
Leslaw Bieniasz wrote:

>
> Cracow, 13.03.2008
>
> Hi,
>
> Assuming that I want to pass a reference to a function, and
> make sure that the function will not make any changes to the object
> passed, is it necessary to declare two "const's" or only one, i.e.
> for example:
>
> void fun(const Type& const x);


should not compile.

>
> or
>
> void fun(const Type& x);


should compile

> Also, is there a difference between
>
> const Type& const x
>
> and
>
> const Type const & x


not really: both are ill-formed, though in different ways. The first applies
a cv-qualifier to a reference type and the second violates [7.1.5/1].


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
Joe Greer
Guest
Posts: n/a
 
      03-13-2008
Leslaw Bieniasz <> wrote in
news:

>
> Cracow, 13.03.2008
>
> Hi,
>
> Assuming that I want to pass a reference to a function, and
> make sure that the function will not make any changes to the object
> passed, is it necessary to declare two "const's" or only one, i.e.
> for example:
>
> void fun(const Type& const x);


This is illegal because references aren't objects and therefore can't be
const by definition (can't modify a non-existant thing in the first
place).

>
> or
>
> void fun(const Type& x);


This is generally sufficient.

>
> Also, is there a difference between
>
> const Type& const x
>
> and
>
> const Type const & x
>


Yes, they are both illegal in different ways. The first tries to make
something which isnt an object const and the second has two const
specifications for Type.

joe

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work??? hn.ft.pris@gmail.com C++ 13 01-22-2007 02:03 PM
pointers to constant characters and constant pointers to characters sam_cit@yahoo.co.in C Programming 4 12-14-2006 11:10 PM
len(var) is [CONSTANT] equal to len(var) == [CONSTANT]? Tor Erik Soenvisen Python 14 11-23-2006 09:57 PM
"Non-constant" constant can't be used as template argument Martin Magnusson C++ 2 10-08-2004 08:41 AM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57