On const reference arguments passing

Discussion in 'C++' started by Giff, Apr 16, 2007.

  1. Giff

    Giff Guest

    Hi,

    I am trying to change my way of programming (I am still learning) , in
    particular I am putting an effort in passing const ref to functions,
    when possible.

    When possible means (to me) when that function is not going to modify
    the object I pass to it.

    Many times though, I create an object, call a function (taking a non-
    const ref) that modifies it somehow and then
    need to use that object as parameter for a function that takes a const
    ref, that will only read the object.

    The compiler complains, since I am passing a non-const object to the
    function and the only thing that I can do is to cast away the
    constness, but is this the right way to go? It feels wrong...

    Thanks for your hints.

    /G
     
    Giff, Apr 16, 2007
    #1
    1. Advertising

  2. Giff

    Giff Guest

    On 16 Apr, 16:12, "Giff" <> wrote:

    Forget my post.
     
    Giff, Apr 16, 2007
    #2
    1. Advertising

  3. Giff wrote:
    > I am trying to change my way of programming (I am still learning) , in
    > particular I am putting an effort in passing const ref to functions,
    > when possible.
    >
    > When possible means (to me) when that function is not going to modify
    > the object I pass to it.


    That's a good rule. Also, when you think of treading the object as
    "a value", it may still be reasonable to pass by reference to const,
    instead of passing by value.

    > Many times though, I create an object, call a function (taking a non-
    > const ref) that modifies it somehow and then
    > need to use that object as parameter for a function that takes a const
    > ref, that will only read the object.


    That sounds reasonable.

    > The compiler complains, since I am passing a non-const object to the
    > function and the only thing that I can do is to cast away the
    > constness, but is this the right way to go? It feels wrong...


    That doesn't sound right. Could you please support this statement
    with code? I can only see such behaviour of the compiler if the
    situation is reversed -- calling a function expecting a ref to non-
    const object from a function where the object is const (i.e. you
    passed the object by reference to const):

    class a {};
    void foo(a& ra);
    void bar(a const & ra) {
    foo(ra); // error
    }
    int main() {
    a object;
    bar(object);
    }

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 16, 2007
    #3
  4. Giff

    Giff Guest

    On 16 Apr, 16:27, "Victor Bazarov" <> wrote:

    > > The compiler complains, since I am passing a non-const object to the
    > > function and the only thing that I can do is to cast away the
    > > constness, but is this the right way to go? It feels wrong...

    >
    > That doesn't sound right.


    I know, sorry for the post. I was doing something else wrong and I
    have just been too impatient.
    My apologies to the group, and thanks for your reply anyway.
     
    Giff, Apr 16, 2007
    #4
    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. Replies:
    11
    Views:
    1,108
  2. Javier
    Replies:
    2
    Views:
    565
    James Kanze
    Sep 4, 2007
  3. George2
    Replies:
    10
    Views:
    599
    Pete Becker
    Dec 17, 2007
  4. 0m
    Replies:
    26
    Views:
    1,122
    Tim Rentsch
    Nov 10, 2008
  5. fungus
    Replies:
    13
    Views:
    892
    fungus
    Oct 31, 2008
Loading...

Share This Page