C++ Constructors

I started learning programming last year and I've been studying .NET since then, recently I decided to try C++ as well.

I have the following code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

class My
{
public:
	int a;

	My()
	{
		this->a = 1;
	}
};

int main()
{
	My m1 = My(); // compiles, m1.a == 1
	My m2; // compiles m2.a == 1
	My m3(); // compiles, but I cannot access m3's field
	return 0;
}


Can someone explain to me what is the difference beween m1 and m2's way of calling the default constructor and why m3 compiles but I cannot access its fields?
Last edited on
http://www.parashift.com/c++-faq/empty-parens-in-object-decl.html
http://en.wikipedia.org/wiki/Most_vexing_parse
http://yosefk.com/c++fqa/

In case you're lazy and don't check out the links, it's because My m3(); is seen as the declaration of a function named m3, returning a My.
Topic archived. No new replies allowed.