Pointers to objects -- passing and returning pointers

This is a discussion on Pointers to objects -- passing and returning pointers within the C++ Programming forums, part of the General Programming Boards category; I'm writing a large numbers class just for fun and currently use no pointers. I basically have objects with functions ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    101

    Pointers to objects -- passing and returning pointers

    I'm writing a large numbers class just for fun and currently use no pointers. I basically have objects with functions for performing operations and these functions return the answer as another LargeNumber. So
    Code:
    (LargeNumber)c = (LargeNumber)a.add((LargeNumber)b);
    will return another (LargeNumber)c that equals a + b. What I want to do is define it to take and return pointers because this would be (supposedly) faster. I was working with it earlier and got a couple errors about scope and pointers so I figure I was doing it wrong.

    Here's my multiply function, it's short because it uses add() and other routines, I added a couple comments.
    Code:
    LargeNumber LargeNumber::mul(LargeNumber x)
    {
      LargeNumber muls[2] = LargeNumber(0,0,0);
      muls[0] = simplemul(x.getDigit(x.getLength()));//simplemul multiples the entire number by a single digit
      muls[0].setSign(1);//sign figured out latter, all positive for addition
      bool yy = 1;
      for(int i = x.getLength() - 1; i > 0; i--)//elementary arithmetic, multiply top by each digit in the bottom...
      {
        muls[yy] = simplemul(x.getDigit(i));
        muls[yy].setSign(1);
        muls[yy].mul10(x.getLength() - i); //adds a zero to the right
        muls[yy] = muls[yy].add(muls[!yy]);
        yy = !yy;
      }
      muls[!yy].setDec(getDec() + x.getDec());//finds the decimal
      if(getSign() == 0 && x.getSign() == 0)
        muls[!yy].setSign(1);
      else
        muls[!yy].setSign(getSign() && x.getSign());
      return muls[!yy];
    }
    I want to turn this into a function that takes a pointer of a LargeNumber and returns a pointer to another LargeNumber.


    Just for simplicity I don't need you to rewrite the entire function. Just pointify this and/or explain something
    Code:
    LargeNumber* LargeNumber::mul(LargeNumber* x)
    {
      LargeNumber muls[2] = LargeNumber(0,0,0); //Define muls[2] somehow...
      muls[0] = simplemul(x.getDigit(x.getLength()));
      //do stuff (I understand x->function()??)
      return muls[yy];
    }
    Code:
    LargeNumber* a = &LargeNumber(...);
    LargeNumber* b = &LargeNumber(...)
    LargeNumber* c = a->mul(b);
    
    //Alternatively I assume this would work??
    
    LargeNumber a = LargeNumber(...);
    LargeNumber b = LargeNumber(...);
    LargeNumber* c = a.mul(&b);
    Last edited by 1veedo; 04-04-2008 at 10:09 AM.
    --

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
      if(getSign() == 0 && x.getSign() == 0)
        muls[!yy].setSign(1);
      else
        muls[!yy].setSign(getSign() && x.getSign());
    will be more correctly as:
    Code:
        muls[!yy].setSign(getSign() != x.getSign());
    That sets sign to 1 if one of the signs is non-zero but the other one is zero.

    Also, if you are going to use pointers, you are probably better off with a three-way-operator function (where one operand is "this", of course), such as this:
    Code:
    void LargeNumber::mul(LargeNumber *x, LargeNumber *res)
    {
    ...
    }
    Just beware that "res" could be the same variable as y or this.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    101
    What's the "proper" way to do this? I've seen code in other projects that make heavy use of pointers to objects. I figure the way I'm currently doing it is inefficient because two object are being copied every time I call a function. You're recommending something like this?
    Code:
    LargeNumber a = LargeNumber(...);
    LargeNumber b = LargeNumber(...);
    LargeNumber c = LargeNumber(...);
    a.mul(&b, &c); //give c the value of a times b
    This isn't the way I always see it being done... The functions of course could somehow be static (like you can do in Java) or not attached to the objects so you could do
    Code:
    LargeNumber.mul(&a, &b, &c); //multiply a and b, give c the value.
    Btw how does new work? http://en.wikipedia.org/wiki/New_(C%2B%2B)
    I'm trying to get a little more in depth with C++.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    I figure the way I'm currently doing it is inefficient because two object are being copied every time I call a function.
    Generally, you can avoid copying by passing by (const) reference. You could then do something like:
    Code:
    LargeNumber LargeNumber::mul(const LargeNumber& rhs) const;
    // ...
    LargeNumber a(...);
    LargeNumber b(...);
    LargeNumber c = a.mul(b);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    I would make mul a free function and use it like this (you could implement it directly or use an idea that follows a common operator+ idiom):
    Code:
    void LargeNumber::multiply_by(const LargeNumber& rhs)
    {
      // implementation ...
    }
    
    LargeNumber multiply(const LargeNumber & lhs, const LargeNumber& rhs)
    {
        LargeNumber temp(lhs);
        temp.multiply_by(rhs);
        return temp;
    }
    
    ...
    
    LargeNumber a(...);
    LargeNumber b(...);
    LargeNumber c = multiply(a, b);
    Note that the return value of your mul function should not be a pointer or reference, because by multiplying two objects you are creating a third object that is the result. You use pointers and references only to objects that already exist.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing and returning array pointers?
    By thealmightyone in forum C Programming
    Replies: 26
    Last Post: 03-26-2009, 03:38 PM
  2. returning pointers
    By spudval in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2007, 02:12 PM
  3. Noob trouble: returning strings (pointers to char)
    By SgtMuffles in forum C Programming
    Replies: 4
    Last Post: 02-28-2005, 07:48 PM
  4. returning an array of pointers to structures
    By dharh in forum C Programming
    Replies: 9
    Last Post: 02-06-2003, 02:26 PM
  5. Passing & Returning Strings from Functions
    By nisaacs in forum C Programming
    Replies: 1
    Last Post: 01-30-2002, 04:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21