Value changes when calling different constructor

Hi,

I've got a bit of an odd problem, I initialize an object, and when I call a different empty constructor, the value of the object changes. What could be the problem?

What do you mean? Like this?
1
2
3
T obj_a;
//...
T obj_b;
And somehow obj_a changes when obj_b is constructed?
closed account (zb0S216C)
What do you mean by changes? Can you give an example, and place the code within a set of [_code_][/_code_] tags (without the underscores)?

Wazzak
Last edited on
1
2
3
4
5
6
T* a;
S* e;
a = new T(3);
cout<<a->getId();//3
e = new S(a);
cout<<a->getId();//something other than, and significantly larger than, 3 


despite the constructor for S being empty.
Can you post the code for T & S?
I had hoped this would be a standard rookie mistake hence neglected to include them, so here they are:

T.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class T;

class T : public R<int, T>
{
	private:
		int* id;
	public:
		T(int id);
		virtual ~T();
			//get/set
		int getId();
		//R methods
		int* getKey();
		T* getValue();
};

T.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
T::T(int id)
{
	this->id = &id;
}

T::~T()
{
}

//get/set
int T::getId()
{
	return *this->id;
}

int* T::getKey()
{
	return this->id;
}

T* T::getValue()
{
	return this;
}


R.h
1
2
3
4
5
6
7
8
9
10
11
12
13
template <class K, class V>
class R
{
	public:
		virtual K* getKey()
		{
			return 0;
		}
		virtual V* getValue()
		{
			return 0;
		}
};


no R.cpp

S.h
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
template<typename K, typename V>
class S
{
	private:
		R<K, V>* s;
		S<K, V>* parent;
		S<K, V>* left;
		S<K, V>* right;
	public:
		S(R<K, V>* s)
		{

		}
		virtual ~S()
		{
			delete this->r;
			delete this->parent;
			delete this->left;
			delete this->right;
		}

		//get/set
		R<K, V>* getR()
		{
			return this->r;
		}
		S<K, V>* getParent()
		{
			return this->parent;
		}
		S<K, V>* getLeft()
		{
			return this->left;
		}
		S<K, V>* getRight()
		{
			return this->right;
		}
};


no S.cpp (because of it being templated)
Last edited on
1
2
3
4
T::T(int id)
{
	this->id = &id;
}


You're making this->id point to a temporary variable. The local id only exists inside that constructor. Once the constructor exits, id no longer exists, and therefore this->id points to nothing/garbage (bad pointer)

Also, your S class should be initializing those pointers in its constructors. If nothing else you should set them to 0 or NULL so that your deletes won't try to delete them.


Longer, more descriptive class names would be good too. It's confusing to try and think of what 'T', 'R' and 'S' are supposed to be.
Thank you Disch, that was indeed the problem.

I had expected this to be a problem that would easily be identified so as to avoid clutter I didn't include all the code, I reproduced it here with shortened names when posting my main function and didn't change them back when posting the rest of the code.

Thanks a lot!
Topic archived. No new replies allowed.