constant references

Discussion in 'C++' started by Leslaw Bieniasz, Mar 13, 2008.

  1. 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
     
    Leslaw Bieniasz, Mar 13, 2008
    #1
    1. Advertising

  2. Leslaw Bieniasz

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Mar 13, 2008
    #2
    1. Advertising

  3. Leslaw Bieniasz

    Joe Greer Guest

    Leslaw Bieniasz <> wrote in
    news:p-kr.edu.pl:

    >
    > 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
     
    Joe Greer, Mar 13, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,111
  2. Martin Magnusson
    Replies:
    2
    Views:
    520
    John Harrison
    Oct 8, 2004
  3. Tor Erik Soenvisen
    Replies:
    14
    Views:
    576
    Tim Roberts
    Nov 23, 2006
  4. Replies:
    4
    Views:
    348
    Keith Thompson
    Dec 14, 2006
  5. Replies:
    13
    Views:
    12,966
    Kai-Uwe Bux
    Jan 22, 2007
Loading...

Share This Page