Array initialization in Class

Hi there

Just want to check about array initialization in Class.

When I tried this

1
2
3
4
5
6
7
8
9
10
11
12
class matrix
{
private:
	double m[2][2];
	

public:
	matrix()
	{
		m[2][2]  = { 0 }; //  array has not been initialized correctly
	}


but I was able to initialize it correctly this way


1
2
3
4
5
6
7
8
9
10
11
12
class matrix
{
private:
	double m[2][2] = { 0 };
	

public:
	matrix()
	{
		m[2][2];
	}


and this way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 class matrix
{
private:
	double m[2][2];
	

public:
	matrix()
	{
		m[0][0] = 0;
                m[0][1] = 0;
                m[1][0] = 0;
                m[1][1] = 0;
	}


what is wrong with the first method?
Last edited on
m[2][2] , when not at the point of creating a 2D array, means "the value of the SINGLE element at the third column of the third row".

So in

m[2][2] = { 0 };

the bit on the left means "the value of the SINGLE element at the third column of the third row".

double m[2][2] means "create a 2D array of doubles".

The problem is that you're simply trying to use syntax that doesn't mean what you want it to.
1
2
3
4
5
6
7
8
9
10
11
12
13
class matrix
{
    private:

        double m[2][2] {} ; // default member initialser
                            // to zero-initialise the array

    public:

        // no user-declared default constructor is needed because
        // the implicitly declared default constructor would use
        // the default member initialser to initialise m to all zeroes
};
Hi JLBorges

thank you for that,

in your you code you don't use = sign, is this due to fact that you had to use in older version of C++? I think C++14 or C++11???

double m[2][2] {} ; vs double m[2][2] = { 0 };

thanks :)
In C++14, all these forms of are allowed for the default member initialser of m; all of them do the same thing.

1
2
3
4
5
6
7
8
double m[2][2] {} ;
double m[2][2] = {} ; 

double m[2][2] {0} ;    // brace-elision:  missing braces around initialisation 
double m[2][2] = {0} ;  // brace-elision:      of sub-object (inner array)

double m[2][2] {{}} ;
double m[2][2] = {{0}} ; 
Cool thank you :)

btw is there website with C++14 or C++17 tutorials? most of the stuff here refers to C++11
in your you code you don't use = sign, is this due to fact that you had to use in older version of C++? I think C++14 or C++11???

Actually it's because he is using one of the recent versions of the C++ standard, C++11 or higher. And realize that C++14 is the current C++ standard, while C++17 is currently a work in progress.

C++11 added what is called uniform initialization, see this link for more information.
http://www.stroustrup.com/C++11FAQ.html#uniform-init
And:
https://www.google.com/search?q=uniform+initialization&ie=utf-8&oe=utf-8
thanks Jlb :)

so much reading and learning so little time :D
> is there website with C++14 or C++17 tutorials?

C++11 was a revolutionary change: "C++11 feels like a new language" - Stroustrup

C++14 is a relatively minor extension to C++11.

Most of the additions by C++17 is to the standard library; other than fundamental changes to rules about evaluation of expressions, most of the other additions to the core language are just syntactic sugar.

Groningen's C++ Annotations is quite up to date; and some sections do mention the upcoming C++17 standard.
http://www.icce.rug.nl/documents/cplusplus/
Thank you JLBorges you are like almanac :)
Topic archived. No new replies allowed.