467,911 Members | 1,401 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,911 developers. It's quick & easy.

How to create new variable in function to return?

100+
Ok, this is my code for my function:

Expand|Select|Wrap|Line Numbers
  1. // Puke - new pointers only point to mTemp
  2. Object &Coordinate::stripObject(std::string name) {
  3.     for ( int i=0; i < mItems.size(); i++ ) {
  4.     if ( mItems.at(i)->getName() == name ) {
  5.         mTemp = *mItems.at(i);
  6.         mItems.erase(mItems.begin() + i);
  7.         return mTemp;
  8.     }
  9.     }
  10.     Object item("Nothing");
  11.     mTemp = item;
  12.     return mTemp;
  13. }
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 :)
Mar 6 '08 #1
  • viewed: 1300
Share:
3 Replies
gpraghuram
Expert 1GB
Ok, this is my code for my function:

Expand|Select|Wrap|Line Numbers
  1. // Puke - new pointers only point to mTemp
  2. Object &Coordinate::stripObject(std::string name) {
  3.     for ( int i=0; i < mItems.size(); i++ ) {
  4.     if ( mItems.at(i)->getName() == name ) {
  5.         mTemp = *mItems.at(i);
  6.         mItems.erase(mItems.begin() + i);
  7.         return mTemp;
  8.     }
  9.     }
  10.     Object item("Nothing");
  11.     mTemp = item;
  12.     return mTemp;
  13. }
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
Expand|Select|Wrap|Line Numbers
  1. void Coordinate::stripObject(std::string name,Object &retObj) {
  2. Object item("Nothing");
  3. retObj = item;
  4. }
  5.  
Raghuram
Mar 6 '08 #2
100+
Thanks for input, but I found a way..

Expand|Select|Wrap|Line Numbers
  1. Object &Character::stripObject(std::string name) {
  2.     for ( int i=0; i < mItems.size(); i++ ) {
  3.     if ( mItems.at(i)->getName() == name ) {
  4.         Object *item = mItems.at(i);
  5.         mItems.erase(mItems.begin() + i);
  6.         return *item;
  7.     }
  8.     }
  9.     std::cout << "Couldn't find " << name << "\n\n";
  10.     Object *item = new Object("Nothing");
  11.  
  12.     return *item;
  13. }
thanks again, though.
Mar 6 '08 #3
gpraghuram
Expert 1GB
Thanks for input, but I found a way..

Expand|Select|Wrap|Line Numbers
  1. Object &Character::stripObject(std::string name) {
  2.     for ( int i=0; i < mItems.size(); i++ ) {
  3.     if ( mItems.at(i)->getName() == name ) {
  4.         Object *item = mItems.at(i);
  5.         mItems.erase(mItems.begin() + i);
  6.         return *item;
  7.     }
  8.     }
  9.     std::cout << "Couldn't find " << name << "\n\n";
  10.     Object *item = new Object("Nothing");
  11.  
  12.     return *item;
  13. }
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
Mar 6 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Andrew V. Romero | last post: by
8 posts views Thread by Steve Neill | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.