syntax questions

what's this mean
 
INPUT input = {0};

and
 
test test1 = {}; //test is a name of a structure 

does it mean like:
1
2
//default constructor of test
test (): variable_of_test(0){}
test test1 = {}; and test test1 = {0}; sets all members of struct to 0


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct test
{
    int a;
    int b;
    char str[20];
};

int main()
{
    test t;
    cout <<t.a<<' '<<t.b<<' '<<t.str; // Will print garbage -1990867164 -2 b◄Ļu╝[’uÓ→@

    cout <<endl;

    test t2={}; // or {0}
    cout <<t2.a<<' '<<t2.b<<' '<<t2.str; // Will print 0 0

    cout <<endl;
    
   // Initisalize each member of struct
    test t3={1,2,"Hello"};
    cout <<t3.a<<' '<<t3.b<<' '<<t3.str; // Will print 1 2 Hello

}


does it mean like:
//default constructor of test
test (): variable_of_test(0){}

I don't know but it can be used to initialize const members:
1
2
3
4
5
6
7
8
9
10
struct test
{
   const int a;
};

int main()
{
    test t; // error: structure with unitialized const members
    test t={10}; // ok
}
Last edited on
I thought that a brace-enclosed initializer list could only be used to initialise a 'plain old data' (i.e. C) struct. So when I see code like

test test1 = {0}

I understand test to be a 'pod' struct, with no need for an actual constructor.

Andy

Of course, C++0x is changing this rule, but my gcc quoted C++98 at me when I tried to use a brace-enclosed initializer list with a class which had a constructor.. I was told to use normal constructor notation.
Last edited on
I don't know but it can be used to initialize const members


it's the same, i just tested it. and afterall, if you could assign const that way, that means you can define const by the default constructor too...
Out of interest, with which compiler?
Topic archived. No new replies allowed.