default constructor

I have the following class defined

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class SearchElement
{
public:
	int m_step_id;
	int m_track_id;
	float m_energy;

	SearchElement()
	{
		int m_step_id = 0;
		int m_track_id = 0;
		float m_energy = 0;
	}
};


Where I have defined a default constructor to initialize the variables. When I call on one of these class members in my main program these members are not initialized to 0. For example,

1
2
3
4
5
6
void main()
{
SearchElement ele;
int energy = ele.m_energy;
cout << energy << endl;
}


yields a garbage value. Why does this not yield 0?
You are initializing local variables in the constructor. Remove the types before the identifiers and it do what you want.
@Peter87 is right. and your code should be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class SearchElement
{
public:
	int m_step_id;
	int m_track_id;
	float m_energy;

	SearchElement()
	{
		m_step_id = 0;
		m_track_id = 0;
		m_energy = 0;
	}
};

then , in your main program you can get the value that you initialized.
Last edited on
Oooops. Of course. Thanks a lot!!
Topic archived. No new replies allowed.