const class reference return from function, what does const do?

Discussion in 'C++' started by Jim Langston, May 11, 2006.

  1. Jim Langston

    Jim Langston Guest

    I'm a little confused. I have a class function declared like:

    const CItem& operator << (const std::string &sIn);

    Which I use like this:

    CItem Item;
    Item << "blaster01,1,0,...,0,0,20,0,0,20,eol";

    At the end of the method it does:
    return *this;

    My question is, is my const on the return value doing anything? I don't
    know why I even have it there (maybe I copied this method from some source).

    const CItem& seems to be to define a reference to a CItem that is constant.
    Is that correct? Item is not declared as const, however. Maybe with the
    way I'm using this thing I don't need to return any value at all anyway?
     
    Jim Langston, May 11, 2006
    #1
    1. Advertising

  2. Jim Langston

    Jim Langston Guest

    "Rolf Magnus" <> wrote in message
    news:e3uq9a$c50$02$-online.com...
    > Jim Langston wrote:
    >
    >> I'm a little confused. I have a class function declared like:
    >>
    >> const CItem& operator << (const std::string &sIn);
    >>
    >> Which I use like this:
    >>
    >> CItem Item;
    >> Item << "blaster01,1,0,...,0,0,20,0,0,20,eol";
    >>
    >> At the end of the method it does:
    >> return *this;
    >>
    >> My question is, is my const on the return value doing anything?

    >
    > Well, you won't be able to chain the operator calls, like:
    >
    > Item << "Hello, " << "world";
    >
    > Basically, returning the reference at all doesn't make much sense if it's
    > const.
    >
    >> I don't know why I even have it there (maybe I copied this method from
    >> some source).
    >>
    >> const CItem& seems to be to define a reference to a CItem that is
    >> constant.
    >> Is that correct?

    >
    > Yes.


    Okay, so this begs the question, should I even be overriding the operator <<
    anyway? If I'm not returning anything, maybe it would be better to just
    make a function like

    void LoadFromString( const std::string& sIn )
     
    Jim Langston, May 11, 2006
    #2
    1. Advertising

  3. Jim Langston

    Rolf Magnus Guest

    Jim Langston wrote:

    > I'm a little confused. I have a class function declared like:
    >
    > const CItem& operator << (const std::string &sIn);
    >
    > Which I use like this:
    >
    > CItem Item;
    > Item << "blaster01,1,0,...,0,0,20,0,0,20,eol";
    >
    > At the end of the method it does:
    > return *this;
    >
    > My question is, is my const on the return value doing anything?


    Well, you won't be able to chain the operator calls, like:

    Item << "Hello, " << "world";

    Basically, returning the reference at all doesn't make much sense if it's
    const.

    > I don't know why I even have it there (maybe I copied this method from
    > some source).
    >
    > const CItem& seems to be to define a reference to a CItem that is
    > constant.
    > Is that correct?


    Yes.
     
    Rolf Magnus, May 11, 2006
    #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. Polanski24
    Replies:
    1
    Views:
    547
    Rolf Magnus
    Apr 11, 2005
  2. Greenhorn
    Replies:
    15
    Views:
    827
    Keith Thompson
    Mar 6, 2005
  3. Javier
    Replies:
    2
    Views:
    565
    James Kanze
    Sep 4, 2007
  4. George2
    Replies:
    10
    Views:
    599
    Pete Becker
    Dec 17, 2007
  5. Arv
    Replies:
    15
    Views:
    926
    James Kanze
    Mar 7, 2008
Loading...

Share This Page