CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2010
    Posts
    3

    Question Variables lost from object when passed to function.

    So I have this object "instruct" with variables of "float amount" and "char * action". When I create an instruct and set the variables I can read from them within the same function, but when I pass it to another function it always "forgets" the amount (but not the action).

    I.e.,

    Code:
    instruct instruction1("LEFT",1);
    instructAr theList;
    theList.Add(instruction1);
    
    
    ...
    
    
    void instructAr::Add(instruct newInstruct)
    {
    	cout << newInstruct.getAction() << " " << newInstruct.getAmount();
    	Array.push_back(newInstruct);
    }
    Any help would be greatly appreciated! Thanks!

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Variables lost from object when passed to function.

    You're passing a copy, not the original object. Does your copy constructor copy the amount?

    Without knowing more about your program, you may be better to be passing by reference or passing a pointer.

  3. #3
    Join Date
    Dec 2010
    Posts
    3

    Re: Variables lost from object when passed to function.

    Here is my copy constructor:

    Code:
    instruct::instruct(const char act[], const int amt)
    {
    	action = new char[1 + strlen(act)];	//ADD ERROR CHECKING
    	strcpy(action,act);
    	amount = amt;
    }
    I shall try passing a pointer and will get back to you!

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Variables lost from object when passed to function.

    Okay, your instruct type apparently contains an owned pointer----this is often a source of trouble. You *can* write code this way, but it's really much easier to make the action member a std::string and forget dynamic char arrays.

    And by the way, the code you posted above is not a copy constructor.

  5. #5
    Join Date
    Dec 2010
    Posts
    3

    Re: Variables lost from object when passed to function.

    Quote Originally Posted by Lindley View Post
    And by the way, the code you posted above is not a copy constructor.
    Argh, had been mistaking that constructor for the copy constructor and had indeed missed out copying over the amount! Such a ridiculous and annoying mistake! Thanks for the help!

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Variables lost from object when passed to function.

    Which is one reason why it's generally better to avoid writing your own copy constructors, and rely on the auto-generated one instead. Of course, this is impossible if your class contains an owned pointer.

  7. #7
    Join Date
    Aug 2008
    Posts
    902

    Re: Variables lost from object when passed to function.

    This is obviously C++, it would arguably be wrong of you to use a char* this way instead of std:string, simply making the switch will fix your problem and allow you to use the default copy constructor.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Variables lost from object when passed to function.

    Quote Originally Posted by GracefulEase View Post
    Here is my copy constructor:
    As others noted, change your type to std::string, and then you don't need the copy constructor. Then the compiler's default copy constructor (and assignment operator) are adequate.

    Secondly, once you are writing your own copy constructor, you are responsible for making sure that the copy is correct, since you are overriding the compiler's responsibility of ensuring correct copies. If there were other member variables you failed to copy, then your copy constructor is faulty. Faulty user-defined copy constructors are a source for very hard-to-find bugs in C++ programs.

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured