Vector of objects (structs) and corrupt values

Hello everyone

I have a question regarding vector of struct objects.

My struct looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
enum ExtremumType 
{ 
	Maximum,
	Minimum
};

struct Extremum
{
	ExtremumType type;
	unsigned int ctm;
	unsigned int index;
	double price;

	Extremum(const ExtremumType _type, const unsigned int _ctm, const unsigned int _index, const double _price)
	{
		type = type;
		ctm = ctm;
		index = index;
		price = price;
	};
};

typedef std::vector<Extremum> Extremums;


The code where I am creating a struct:
1
2
3
4
5
	Extremums rateExtremums;


	rateExtremums.push_back(Extremum(Maximum, 1111, 111, 11));
	rateExtremums.push_back(Extremum(Minimum, 2222, 222, 22));


Problem is that just when this code get executed, the values in this vectore gets corrupted like this:
http://img15.imageshack.us/img15/9681/capturevector1.png
I.E. values for variables are nothing like I have specified in those two lines. Guess the answer to this one is pretty simple, but because of being new to c++, I just don't know how to google for it ;)
Last edited on
Your constructor is buggy:

1
2
3
	Extremum(const ExtremumType _type, const unsigned int _ctm, const unsigned int _index, const double _price)
	: type(_type), ctm(_ctm), index(_index), price(_price)
	{}
I guess I was writing this code asleep :D

Just one additional question.
1
2
3
4
5
6
7
	Extremum(const ExtremumType _type, const unsigned int _ctm, const unsigned int _index, const double _price)
	{
		type = _type;
		ctm = _ctm;
		index = _index;
		price = _price;
	};


Is the above constructor in some ways worse then the one you have shown? What is the purpose for this constructor notation you are using?
Last edited on
In this specific case, probably not. In general, yes; initializing members using a member initialization list is better than using assignment.

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6

If you get a job as a C++ programmer, you will be expected to use member initialization lists. It's easier to develop good habits now while you are learning the language than to break bad habits later.
Topic archived. No new replies allowed.