Constructor "styles"

Hello people.
What's the difference between these two constructors? Which one should I use and for what benefit? Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
class B {
	public:
	int _a;
	B(int a) {
		_a = a;
	}
};

class C {
	public:
	int _a;
	C(int a): _a(a) {}
};

Last edited on
they are different. you can't choose what to use between them because they assign to different variable (though it has the same name: _a, but it refer to different class). the usage of these constructors is depend on you, which variable you intend to create.
closed account (z05DSL3A)
In the first one, when a B is constructed it first constructs the _a with its (_a's) default constructor and then assignees a value to it.

In the second one the _a is constructed with the correct value to start with.
closed account (DSLq5Di1)
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6
Topic archived. No new replies allowed.