How to create new variable in function to return?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zensunni
    New Member
    • May 2007
    • 101

    How to create new variable in function to return?

    Ok, this is my code for my function:

    Code:
    // Puke - new pointers only point to mTemp
    Object &Coordinate::stripObject(std::string name) {
        for ( int i=0; i < mItems.size(); i++ ) {
    	if ( mItems.at(i)->getName() == name ) {
    	    mTemp = *mItems.at(i);
    	    mItems.erase(mItems.begin() + i);
    	    return mTemp;
    	}
        }
        Object item("Nothing");
        mTemp = item;
        return mTemp;
    }
    This, of course, is not what I want to do. I want to create a brand new variable and return it. Every time I do this, however, g++ gives an error (probably because the created variable falls out of scope)

    BTW - Any variable with an 'm' as the fist character is a member variable.

    Any help would be greatly appreciated :)
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by zensunni
    Ok, this is my code for my function:

    Code:
    // Puke - new pointers only point to mTemp
    Object &Coordinate::stripObject(std::string name) {
        for ( int i=0; i < mItems.size(); i++ ) {
    	if ( mItems.at(i)->getName() == name ) {
    	    mTemp = *mItems.at(i);
    	    mItems.erase(mItems.begin() + i);
    	    return mTemp;
    	}
        }
        Object item("Nothing");
        mTemp = item;
        return mTemp;
    }
    This, of course, is not what I want to do. I want to create a brand new variable and return it. Every time I do this, however, g++ gives an error (probably because the created variable falls out of scope)

    BTW - Any variable with an 'm' as the fist character is a member variable.

    Any help would be greatly appreciated :)

    Instead of returning the local object pass a variable as an argument(by reference) and then initialize it
    [code=cpp]
    void Coordinate::str ipObject(std::s tring name,Object &retObj) {
    Object item("Nothing") ;
    retObj = item;
    }
    [/code]

    Raghuram

    Comment

    • zensunni
      New Member
      • May 2007
      • 101

      #3
      Thanks for input, but I found a way..

      Code:
      Object &Character::stripObject(std::string name) {
          for ( int i=0; i < mItems.size(); i++ ) {
      	if ( mItems.at(i)->getName() == name ) {
      	    Object *item = mItems.at(i);
      	    mItems.erase(mItems.begin() + i);
      	    return *item;
      	}
          }
          std::cout << "Couldn't find " << name << "\n\n";
          Object *item = new Object("Nothing");
      
          return *item;
      }
      thanks again, though.

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by zensunni
        Thanks for input, but I found a way..

        Code:
        Object &Character::stripObject(std::string name) {
            for ( int i=0; i < mItems.size(); i++ ) {
        	if ( mItems.at(i)->getName() == name ) {
        	    Object *item = mItems.at(i);
        	    mItems.erase(mItems.begin() + i);
        	    return *item;
        	}
            }
            std::cout << "Couldn't find " << name << "\n\n";
            Object *item = new Object("Nothing");
        
            return *item;
        }
        thanks again, though.
        Using pointers is another option but the idea i told you will avoid the overhead of deleting the pointer in the calling part.

        Raghuram

        Comment

        Working...