Thread: Passing Objects using menus

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    Passing Objects using menus

    Can anyone please explain/point out a good example of how to create and pass object(s) using a menu. I seem to find this a hard concept to fully understand. I will post the menu portion of my code below. I do not think the entire code is needed, but if required, I can re-post it. I seem to lose the object created, thereby am unable to do anything with the created object. Can anyone help?

    Code:
     int selection;
    ArrayStack stack(0);
    	
    do
    {
        selection = mainMenu();
        switch (selection)
       {	
          int size, number;
          case 1: 
          {
              cout << "How many elements do you want the Stack to 
                             have? ";
              cin  >> size;
              createStack(stack, size);
              break;
           }
           case 2: 
           {
                cout << "Enter the number to Add(Push) onto the Stack: ";
                cin  >> number;
                pushStack(stack, number);
                break;
           }
           case 3:
           {
               cout << "Enter the number to Remove(Pop) off the 
                              Stack: ";
               cin  >> number;
               popStack(stack, number);
               break;					   
           }
           case 4: CLS;
           	    cout <<  endl 
      	    << "Press the <ENTER> key to Exit the Program" 
                        << endl << endl;
                        exit(EXIT_SUCCESS);
    	    break;
    }//end switch
    
    cout << endl << endl
    << "Press the <ENTER> key to return to the main menu.";
    cin.get();
    
    }while(selection != 4);
    The three functions used in the menu:
    createStack, pushStack, popStack
    are passed a reference of the ArrayStack class and are declared in the main function, they are not part of the class.
    I have tried every way that I can think of but seem to be unable to pass an object through the entire program without apparently losing it.

    Any suggestions?
    Thanks,
    Alan

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Are you passing by value, or by reference (using pointers)?

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    passing objects

    I was trying to pass by reference but am open to ideas. I believe that once I get the comcept down I can expand from there. Anyway, here are the function declarations for the functions that are not part of the class.

    ArrayStack & createStack(int);
    void pushStack(ArrayStack &, int);
    void popStack(ArrayStack &, int);
    void displayStack(ArrayStack &);

    I don't think that the code is really too much of the issue. If anyone knows where examples of this type of problem are please pass them on. Most books show stupid examples of code like such:
    Code:
    ArrayStack stack(5);
    	int catchVar;
    
    	cout << "Pushing 5" << endl;
    	stack.push(5);
    	cout << "Pushing 10" << endl;
    	stack.push(10);
    	cout << "Pushing 15" << endl;
    	stack.push(15);
    	cout << "Pushing 20" << endl;
    	stack.push(20);
    	cout << "Pushing 25" << endl;
    	stack.push(25);
    
    	cout << "Popping..." << endl;
    	stack.pop(catchVar);
    	cout << catchVar << endl;
    	stack.pop(catchVar);
    	cout << catchVar << endl;
    	stack.pop(catchVar);
    	cout << catchVar << endl;
    	stack.pop(catchVar);
    	cout << catchVar << endl;
    	stack.pop(catchVar);
    	cout << catchVar << endl;
    this is fine to understand how to call the push/pop functions and it works. But we go back to the original question, how are functions passed during the programs running so that the object is not lost and that the object may be changed/called to do something?
    Thanks,
    Alan

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Hmmm, something doesn't quite add up. In your first code snippet, you create an ArrayStack with this line:

    ArrayStack stack(0);

    Then, you show a function called createStack like this:

    ArrayStack & createStack(int);

    So which way do you create the stack? It depends on how you implemented it, but I have a feeling createStack might not work the way you expect it. If you create the stack on the stack (two totally different stacks) inside the function, then return a reference to it, the result will be bad because the stack created inside the function will go out of scope when the function ends and be destroyed.

    Please post the createStack code and the exact code that calls it. That is the only thing I see in your code so far that might be a problem. The rest of the pass-by-reference function declarations look ok.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing local objects by reference
    By manav in forum C++ Programming
    Replies: 15
    Last Post: 03-31-2008, 07:32 AM
  2. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  3. passing objects
    By r0bbb in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2005, 01:10 PM
  4. Passing objects
    By xshapirox in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2004, 11:34 AM
  5. Passing objects
    By Wiggin in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2001, 08:00 AM