LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   pass pointer,, return pointer??? (http://www.linuxquestions.org/questions/programming-9/pass-pointer-return-pointer-346007/)

blizunt7 07-23-2005 11:48 AM

pass pointer,, return pointer???
 
Hey all,
I am tryin to cut down the size of my program by creating a new class to perform certain operations.
I would like to declare an array in my main, pass the array to EACH function in the new class, and have some value be inserted into the same array and be saved.

My main is such:
Code:

SoldItem *sold = new SoldItem[5];
int main()
{  some code }

void mainFunction()
{
  OperateSold *OS = new OperateSold();  // My helper function

  OS->amountSold(sold);
  sold[i].getAmountSold();  // will this return the same aount set?
}

THe OperateSold class would have many functions, such as:
Code:

OperateSold::OperateSold(){    }
OperateSold::~OperateSold() {  }

void OperateSold::amountSold(SoldItem *sold)
{
    char amount;
    cout<< "Enter amount: ";
    cin >> amount;
    sold[i].setAmountSold(amount);
  ....code.....


}

Will this properly insert the char amount into the sold array, and if the sold array is reference in main after the amountSOld function call, will the amount be in the same sold array, or somewhere else in memory.

DO i need to return a pointer to the array??
Or is the passing SoldItem pointer going to insert the char amount in the proper memory location for the array.

THanks very much

aluser 07-23-2005 11:55 AM

You don't need to return the pointer; the way you're doing it should work.

It might be cleaner to have the sold array be a member of the OperateSold object and only access it though that class. That way you don't end up with a global array that magically changes when you call functions on the OS object (counterintuitive).

blizunt7 07-23-2005 01:28 PM

HMM interesting point,


THe only issue is, that i need to referende the sold array in other functions in the main class.

Can i have a function in OperateSold, that will return the array so i can use sold[i] in other functions???

aluser 07-23-2005 01:36 PM

of course. Another way to do it would be a function like
Code:

SoldItem* OperateSold::getSoldItem(int i) { return &sold[i]; }
(and for people anal about effeciency, make it an inline ;) )


Doesn't really matter though. whatever looks right to the maintainer (you!) is best :D


All times are GMT -5. The time now is 11:38 PM.