-
December 1st, 2010, 02:14 PM
#1
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!
-
December 1st, 2010, 02:25 PM
#2
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.
-
December 1st, 2010, 02:29 PM
#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!
-
December 1st, 2010, 02:35 PM
#4
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.
-
December 1st, 2010, 02:45 PM
#5
Re: Variables lost from object when passed to function.
 Originally Posted by Lindley
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!
-
December 1st, 2010, 02:57 PM
#6
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.
-
December 1st, 2010, 07:05 PM
#7
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.
-
December 1st, 2010, 08:48 PM
#8
Re: Variables lost from object when passed to function.
 Originally Posted by GracefulEase
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|