Return pointer or variable by ref.?

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

    Return pointer or variable by ref.?

    Hello I would like you guys to give me the best solution for this problem:


    class bigInteger {
    private:
    vector<char> digits;
    public:
    ...

    bigInteger& operator+=(cons t bigInteger& x)
    { ... } // Already implemented.


    // WHICH WAY DO I CHOOSE?

    // Option 1
    bigInteger operator+(const bigInteger& x) const
    {
    bigInteger y = *this; // ONE COPY
    y += x;
    return y; // TWO COPIES (?)
    }

    // Option 2
    bigInteger* operator+(const bigInteger& x) const
    {
    bigInteger y = new bigInteger(*thi s);
    *y += x;
    return y;
    } // ONLY ONE COPY, but I wouldn't like to avoid pointers, and I am
    using references, pointers, and static variables mixed up everywhere.

    // Option 3
    bigInteger& operator+(const bigInteger& x) const
    {
    bigInteger y = new bigInteger(*thi s);
    *y += x;
    return *y;
    } // It isn't OK. Now I miss garbage collector!

    // Option 4
    void add(const bigInteger& op, bigInteger& out)
    {
    out = *this;
    out += op;
    } // OK, but can't use operator+ and build arithmetic expressions

    };


    If you have any better solution, tell me!

    Thanks.
  • Nafai

    #2
    Re: Return pointer or variable by ref.?

    [color=blue]
    > } // ONLY ONE COPY, but I wouldn't like to avoid pointers, and I am
    > using references, pointers, and static variables mixed up everywhere.[/color]

    I meant: ... but I _would_ like to avoid pointers ...

    Comment

    • Karl Heinz Buchegger

      #3
      Re: Return pointer or variable by ref.?

      Nafai wrote:[color=blue]
      >
      >
      > If you have any better solution, tell me![/color]

      Let the optimizer do its work

      bigInteger& operator+(const bigInteger& x) const
      {
      bigInteger y( *this );
      y += x;
      return y;
      }

      --
      Karl Heinz Buchegger
      kbuchegg@gascad .at

      Comment

      • Nafai

        #4
        Re: Return pointer or variable by ref.?

        Karl Heinz Buchegger escribió:[color=blue]
        > Nafai wrote:
        >[color=green]
        >>
        >>If you have any better solution, tell me![/color]
        >
        >
        > Let the optimizer do its work
        >
        > bigInteger& operator+(const bigInteger& x) const
        > {
        > bigInteger y( *this );
        > y += x;
        > return y;
        > }
        >[/color]

        But is it OK to return a local variable?

        Comment

        • Nafai

          #5
          Re: Return pointer or variable by ref.?

          [color=blue][color=green][color=darkred]
          >>> If you have any better solution, tell me![/color]
          >>
          >>
          >>
          >> Let the optimizer do its work
          >>
          >> bigInteger& operator+(const bigInteger& x) const
          >> {
          >> bigInteger y( *this );
          >> y += x;
          >> return y;
          >> }
          >>[/color]
          >
          > But is it OK to return a local variable?[/color]

          But is it OK to return a local variable _by reference_?

          Comment

          • Karl Heinz Buchegger

            #6
            Re: Return pointer or variable by ref.?

            Nafai wrote:[color=blue]
            >
            > Karl Heinz Buchegger escribió:[color=green]
            > > Nafai wrote:
            > >[color=darkred]
            > >>
            > >>If you have any better solution, tell me![/color]
            > >
            > >
            > > Let the optimizer do its work
            > >
            > > bigInteger& operator+(const bigInteger& x) const
            > > {
            > > bigInteger y( *this );
            > > y += x;
            > > return y;
            > > }
            > >[/color]
            >
            > But is it OK to return a local variable?[/color]

            Ooops. Sorry. I didn't want to write that. It happend
            during Cut&Paste. You are right of course. The return
            type is object, not reference to object.

            bigInteger operator+(const bigInteger& x) const
            {
            bigInteger y( *this );
            y += x;
            return y;
            }

            --
            Karl Heinz Buchegger
            kbuchegg@gascad .at

            Comment

            • Nafai

              #7
              Re: Return pointer or variable by ref.?

              Karl Heinz Buchegger escribió:[color=blue]
              > Nafai wrote:
              >[color=green]
              >>Karl Heinz Buchegger escribió:
              >>[color=darkred]
              >>>Nafai wrote:
              >>>
              >>>
              >>>>If you have any better solution, tell me!
              >>>
              >>>
              >>>Let the optimizer do its work
              >>>
              >>> bigInteger& operator+(const bigInteger& x) const
              >>> {
              >>> bigInteger y( *this );
              >>> y += x;
              >>> return y;
              >>> }
              >>>[/color]
              >>
              >>But is it OK to return a local variable?[/color]
              >
              >
              > Ooops. Sorry. I didn't want to write that. It happend
              > during Cut&Paste. You are right of course. The return
              > type is object, not reference to object.
              >
              > bigInteger operator+(const bigInteger& x) const
              > {
              > bigInteger y( *this );
              > y += x;
              > return y;
              > }
              >[/color]

              So I may suppose that there will be only one copy thanks to the
              optimizer. Am I right?

              Comment

              • Karl Heinz Buchegger

                #8
                Re: Return pointer or variable by ref.?

                Nafai wrote:[color=blue]
                >
                > Karl Heinz Buchegger escribió:[color=green]
                > > Nafai wrote:
                > >
                > > bigInteger operator+(const bigInteger& x) const
                > > {
                > > bigInteger y( *this );
                > > y += x;
                > > return y;
                > > }
                > >[/color]
                >
                > So I may suppose that there will be only one copy thanks to the
                > optimizer. Am I right?[/color]

                The language standard has nothing to say about that. But in
                practice, yes, the optimizer will do a good job and optimize
                away the local variable. In fact, if the above function gets inlined,
                and there is no reason that it won't, it is one of the simpler
                tasks of the optimizer to get rid of it.

                --
                Karl Heinz Buchegger
                kbuchegg@gascad .at

                Comment

                • Nafai

                  #9
                  Re: Return pointer or variable by ref.?

                  Karl Heinz Buchegger escribió:[color=blue]
                  > Nafai wrote:
                  >[color=green]
                  >>Karl Heinz Buchegger escribió:
                  >>[color=darkred]
                  >>>Nafai wrote:
                  >>>
                  >>> bigInteger operator+(const bigInteger& x) const
                  >>> {
                  >>> bigInteger y( *this );
                  >>> y += x;
                  >>> return y;
                  >>> }
                  >>>[/color]
                  >>
                  >>So I may suppose that there will be only one copy thanks to the
                  >>optimizer. Am I right?[/color]
                  >
                  >
                  > The language standard has nothing to say about that. But in
                  > practice, yes, the optimizer will do a good job and optimize
                  > away the local variable. In fact, if the above function gets inlined,
                  > and there is no reason that it won't, it is one of the simpler
                  > tasks of the optimizer to get rid of it.
                  >[/color]

                  OK, thank-you.

                  Comment

                  Working...