static counter and = operator

Im working on a class that has four different types. In the class i have static integers to act as a counter, so that each time an instance of the class is created the counter corresponding to the type of the new object increases. When I construct using the intialising constructor, the counter increases by one depending on the type, whereas for the default constructor, I increase a counter for the number of uninitialised instances.

I've come unstuck though with the = operator, which i have already overloaded, as the way the counterrs are changed is quite complicated, and i cant see how to make this work. I've thought about it so there seems to me four situations:

1
2
3
4
5
6
7
8
9
myclass one(myType a);		// An initialised instance 
myclass two(myType b);		
myclass three();		// An uninitialised instance
myclass four();

two = one;			// 1) Assign initialised to initialised
three = one;			// 2) Assign initialised to uninitialised
one = myclass(myType c);	// 3) Assign unsaved instance to initialised
four = myclass(myType d);	// 4) Assign unsaved instance to unitialised 


The problem really is that the 3rd and 4th situations use the constructor to assign to the variables, so that the counter has already increased by the time the assignment is made. I need to know if there is a way around this, or if there isn't, whether i can get info as to whether the instance on the rhs has been saved or not.

I started writing this question earlier then sort of solved it, but i changed something in my code, which of course screwed it all up now and i cant remember for the life of me what i'd done... Any advise would be greatly welcomed.. Thankeee
I'd have to see a complete declaration of the class to truly understand what you are trying to do. When you say that you create a class with 4 different types it sounds like you are just overloading the constructor. Is it a template class? What is being initialized in each constructor? If the class state can be changed then I would wonder what you are doing with those counters in those cases and I would like to see the body of the constructor. Without more info your question seems really bizarre.
Yeah i tried to put that as clearly as possible but i dont think i really managed.
Essentially i have a class, that contains a variable that can be one of 4 values, plus an un-initialised value. The counters should then give the number of instances of a class where the variable is of the value corresponding to that variable.

Anyway that's perhaps a better description, as for code, the class is fairly big, so hopefully an abridged version will be enough. It uses another class though, called two_vector, that essentially holds two integers and has = and + operators overloaded. I dont know if you will, but i can give the code for that as well, if you fancy testing the code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class segment {
public:
	char type;
	two_vector start,dir;
	static int L;
	static int U;
	static int x;
	
	two_vector get_end() { return start+dir; }
	segment();
	segment (char, two_vector);
	segment (char, int, int);
	segment& operator= (const segment&) ;
	void increment_counter(char, int);
	void change_dir(char);
};

int segment::L = 0;
int segment::U = 0;
int segment::x = 0;

segment::segment(){
	type ='x';
	start = two_vector(-1,-1);
	dir = two_vector(0,0);
	x++;
}

segment::segment (char direction, two_vector start_point){
	start = start_point;
	change_dir(direction);
	increment_counter(direction,1);
}

segment::segment (char direction, int a, int b){
	start = two_vector(a,b);
	change_dir(direction);
	increment_counter(direction,1);
}

segment& segment::operator= (const segment& rhs) {
	this->start= rhs.start;
	if(this->type == 'x'){			// works for assigning saved instance to unintiatied *this
		change_dir(rhs.type);
		increment_counter(this->type,1);
	} else {						// works for assigning saved instance to initiated *this
		increment_counter(this->type,-1);
		change_dir(rhs.type);
		increment_counter(this->type,1);
	}	
	return *this;
}

void segment::change_dir(char direction){
	switch (direction){
		case 'L':
			type = direction;
			dir = two_vector(-1,0);
			break;
		case 'U':
			type = direction;
			dir = two_vector(0,-1);
			break;
		default:
			std::cout << "Incorrect segment direction assignment.  Direction given was: "<< direction <<std::endl;
			break;
	}
}

void segment::increment_counter(char direction, int increment) {
	switch (direction){
		case 'L':
			L+= increment;
			break;
		case 'U':
			U+= increment;
			break;
		case 'x':
			x+= increment;
			break;	
		default:
			std::cout << "failure to incrmement counter as type passed was: "<<direction<<std::endl;
			break;	
	}
}


Hopefully I've not done anything too crazy to understand. The operator= definition wil curently work correctly for the counter's given the assignment operator is used under a circumstance such as in 1 or 2 in my original post (where the segment on the right is saved) but cannot work for the cases where the rhs is a constructor. The actual values of the class should be affected correctly by the operator nonetheless.

Also, just as it might help to have some context, i'm trying to build a program that solves slitherlink puzzles. A segment 'type' would normally also be able to be R and D, but i removed all uses in the code as hopefully the principle is still clear.. Hopefully..
Topic archived. No new replies allowed.