A doubt on assignment operator.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Amit Bhatia

    A doubt on assignment operator.

    Hi,
    I am trying to define the assignment operator for a class called
    Cell. I am bit confused between the following two statements:

    const Cell& Cell::operator= (const Cell &r)
    {
    \\ all the stuff

    }

    and,

    Cell& Cell::operator= (const Cell &r)
    {
    \\ all the stuff

    }

    Is the first work going to work at all? does it make any sense? I think
    it is not going to do what it is intended to do.

    thanks,
    --a.

  • Moritz Beller

    #2
    Re: A doubt on assignment operator.

    On 26 Sep 2005 11:55:12 -0700
    "Amit Bhatia" <amit.bhatia@gm ail.com> wrote:
    [color=blue]
    > Is the first work going to work at all? [/color]

    Try it out for yourself.
    [color=blue]
    > does it make any sense? [/color]

    Well, let's try and figure out what the definition const Cell&
    Cell::operator= (const Cell &r) actually means: It says that the
    assignment operator shall return a constant reference of type Cell.
    It's usually of no great importance whether you have both a const and
    non-const assignment operator -- you will bearly use the object
    returned by it.

    It does however make sense for an (merely used) if-statement such as
    if((Cell1=Cell2 ).changeContent ("a"))
    when you don't want Cell1's content to be changed.
    [color=blue]
    > I think it is not going to do what it is intended to do.[/color]

    You don't mention what you intend to do, hence I can't tell you. FYI:
    Cell& Cell::operator= (const Cell &r) const is certainly rubbish.

    best regards / Gruß
    Moritz Beller
    --
    web http://www.4momo.de
    mail momo dot beller at t-online dot de
    gpg http://gpg.notlong.com

    Comment

    • Victor Bazarov

      #3
      Re: A doubt on assignment operator.

      Amit Bhatia wrote:[color=blue]
      > I am trying to define the assignment operator for a class called
      > Cell. I am bit confused between the following two statements:
      >
      > const Cell& Cell::operator= (const Cell &r)
      > {
      > \\ all the stuff
      >
      > }
      >
      > and,
      >
      > Cell& Cell::operator= (const Cell &r)
      > {
      > \\ all the stuff
      >
      > }
      >
      > Is the first work going to work at all? does it make any sense? I think
      > it is not going to do what it is intended to do.[/color]

      The difference between the two is the return value type, right? The
      return value type is different only in the const-ness of the object to
      which the returned reference points, right? So, why would the first
      one *not work*? When you write the expression

      someCell = someOtherCell;

      it's the same as

      someCell.operat or=(someOtherCe ll);

      which basically _discards_ the return value. Whatever happens between
      passing the argument and returning a value, IOW, whatever happens between
      the curly braces of the function, is the same, no? So, how it may not
      work if they are doing exactly the same?

      V

      Comment

      • Moritz Beller

        #4
        Re: A doubt on assignment operator.

        On Mon, 26 Sep 2005 21:13:05 +0200
        Moritz Beller <momo.beller. No-Spam@t-online.de> wrote:
        [color=blue]
        > It's usually of no great importance whether you have both a const and[/color]
        xx --> or[color=blue]
        > non-const assignment operator -- you will bearly use the object[/color]

        Who put the "both ... and" in there? ;)

        best regards / Gruß
        Moritz Beller
        --
        web http://www.4momo.de
        mail momo dot beller at t-online dot de
        gpg http://gpg.notlong.com

        Comment

        • Greg

          #5
          Re: A doubt on assignment operator.


          Victor Bazarov wrote:[color=blue]
          > Amit Bhatia wrote:[color=green]
          > > I am trying to define the assignment operator for a class called
          > > Cell. I am bit confused between the following two statements:
          > >
          > > const Cell& Cell::operator= (const Cell &r)
          > > {
          > > \\ all the stuff
          > >
          > > }
          > >
          > > and,
          > >
          > > Cell& Cell::operator= (const Cell &r)
          > > {
          > > \\ all the stuff
          > >
          > > }
          > >
          > > Is the first work going to work at all? does it make any sense? I think
          > > it is not going to do what it is intended to do.[/color]
          >
          > The difference between the two is the return value type, right? The
          > return value type is different only in the const-ness of the object to
          > which the returned reference points, right? So, why would the first
          > one *not work*? When you write the expression
          >
          > someCell = someOtherCell;
          >
          > it's the same as
          >
          > someCell.operat or=(someOtherCe ll);
          >
          > which basically _discards_ the return value. Whatever happens between
          > passing the argument and returning a value, IOW, whatever happens between
          > the curly braces of the function, is the same, no? So, how it may not
          > work if they are doing exactly the same?
          >
          > V[/color]

          The original poster probably can't tell whether either function works
          because they won't compile. The two functions differ only by their
          return type - making one an illegal overload of the other if both are
          declared.

          Greg

          Comment

          Working...