Is it possible to copy a temporary class instance of itself into itself?

Sep 9, 2011 at 8:49pm
I have several bool validation methods in a class that will set data if it is valid, otherwise it does not change anything and returns false.

I need to write a method that will set all of the classes data using these bool validation methods but only if ALL of them return true. If even one of them returns false the entire class instance must remain unchanged.

I was thinking of a creating a temp instance of the class inside the method and if all of the validation methods return true than I would copy the temp instance into the current instance.

But I'm not sure how to do that without manually copying member by member. Is there a way to use the copy constructor on the current class from within itself?

Or is there a better way to do it?
Sep 9, 2011 at 9:21pm
closed account (zwA4jE8b)
seems like you could just make a pointer of your class type. initialize it using new. use it. then use delete

1
2
3
4
5
6
7
8
9
10
11
12
13
class boolclass
{}

int main()
{
  boolclass* temp = new boolclass;

//manipulate temp

delete temp;

return 0;
}
Sep 9, 2011 at 11:07pm
Yea, but this has to be a method and not a top level function. Am I going to have to just copy all the data members manually?
Sep 10, 2011 at 12:18am
Bjarne Stroustrup wrote:
Code that creates an object using new and then deletes it at the end of the same scope is ugly, error-prone, and inefficient.


I have several bool validation methods in a class that will set data if it is valid
It seems that you don't want those side effects, ¿why did you implement them?

1
2
3
some_class tmp = a; //copy constructor
/* work with a (or tmp)*/
a = tmp; //rollback (or commit) 
Sep 10, 2011 at 5:12am
I know it's dumb but it's my first program in OOP in C++.

You would think that this would be a top-level function because it makes so much more sense that way but I guess I will just have to copy each data member one by one because it doesn't seem that you can copy a class into the current class inside that current class.

If only the keyword "this" could be used for assignment purposes I could just write:

if (all validation methods return true) {
this = temp_class;
return true;
}
else
return false;


That would be so slick.
Last edited on Sep 10, 2011 at 5:15am
Sep 10, 2011 at 5:31am
closed account (1yR4jE8b)
if (all validation methods return true) {
this = temp_class;
return true;
}
else
return false;


can't you just use the assignment operator?

1
2
3
4
5
6
7
Class tmp = temporary_instance;

if (/*validation all checks out*/) {
  *this = tmp;
  return true;
}
return false;


You may neeed to implement your own operator=() for your class if it manages dynamic memory -- if it does you should have already done this. If it doens't manage any dynamic memory, the compiler will automatically generate an assignment operator for you.
Last edited on Sep 10, 2011 at 5:33am
Sep 10, 2011 at 5:24pm
Hey thanks, I wrote my own copy constructor and it works fine but when I actually try the:


*this = temp;



I get a seg fault. Does anyone have any idea why? I narrowed it down to my array and vector
but it doesn't look like I should be going out of bounds.

Here is the code for my copy constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

CreatureClass& CreatureClass::operator = (const CreatureClass& t) {
	if (this != &t) {
		delete abilities;					// delete dynamic data
		abilities = new int[AbilityMax];	// grab new cells for it
	
		for (int i = 0; i < t.AbilityMax; i++)	// copy abilities 
			abilities[i] = t.abilities[i];		
		
		for (int i = 0; i < t.skills.size(); i++)	// copy skills
			skills[i] = t.skills[i];	

		nickname = t.nickname;
		race = t.race;
		vocation = t.vocation;
		health = t.health;
		dob = t.dob;
	}
	
	return *this;
Last edited on Sep 10, 2011 at 5:38pm
Sep 10, 2011 at 5:41pm
The problem is with this sentence "delete abilities".
It should be "delete []abilities".

It is ok to write "*this = tmp".
Good Luck:)
Sep 11, 2011 at 12:54am
chenqi07, thanks for finding that error for me.

When I went to fix it I completely forgot that skills was a vector so by changing

1
2
for (int i = 0; i < t.skills.size(); i++)	// copy skills
			skills[i] = t.skills[i];	


TO

1
2
for (int i = 0; i < t.skills.size(); i++)	// copy new abilities 
			skills.push_back(t.skills[i]);	


the seg fault went away.
Thanks alot!
Last edited on Sep 11, 2011 at 12:56am
Topic archived. No new replies allowed.