Thread: Pointers to objects -- passing and returning pointers

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.
    --

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, 08:48 PM
  4. returning an array of pointers to structures
    By dharh in forum C Programming
    Replies: 9
    Last Post: 02-06-2003, 03:26 PM
  5. Passing & Returning Strings from Functions
    By nisaacs in forum C Programming
    Replies: 1
    Last Post: 01-30-2002, 05:34 AM