den_v
When you create an instance of a "class" with "new", it also initialized its members to "0", similar to what "{}" does to structs.
No, it does not. Well, not always. I depends on multiple things for this to happen.
Disclaimer: all stuff written below is for C++03 and newer C++ standards. For C++98 the behavior is a bit different. But nobody uses C++98 anymore, so lets ignore it.
Here's an example where you can see that struct and class behaves exactly the same for member initialization:
1
2
3
4
5
6
7
8
9
10
11
12
13 | #include <stdio.h>
class Class { public: int x; };
struct Struct { int x; };
int main()
{
Class* c = new Class;
printf("%i\n", c->x);
Struct* s = new Struct;
printf("%i\n", s->x);
}
|
And when I compile and execute it:
| C:\test>g++ test.cpp -o test
C:\test>test.exe
38675600
14312016
|
You may be lucky and compiler has initialized heap memory with 0 or placed both objects in place where memory contents is 0, then of course you won't see random values like here. But you cannot rely on that. At some point compiler will start placing objects on heap that reuses previously used memory which contains non-zero values and then you'll see garbage values like the example above.
struct and class differs only in two properties - default inheritance for struct is public, and default visibility for members is also public. There are no other differences between struct and class. Everything else is the same. Both can have construtors/destructors, both can be inherited in whatever way you want, both can have virtual or pure virtual functions. Both can be used as types for templates or whatever else.
You can get 0-initialized struct and class POD members if you have auto-generated constructor and make new operator to use it:
1
2
3
4
5
6
7
8
9
10
11
12
13 | #include <stdio.h>
class Class { public: int x; };
struct Struct { int x; };
int main()
{
Class* c = new Class(); // <-- this will make compiler to use auto generated constructor
printf("%i\n", c->x);
Struct* s = new Struct(); // <-- this will make compiler to use auto generated constructor
printf("%i\n", s->x);
}
|
Compile and run it:
| C:\test>g++ test.cpp -o test
C:\test>test.exe
0
0
|
But if you have an explicit constructor, then again no POD types will be initialized automatically:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 | #include <stdio.h>
class Class {
public:
Class() {}
int x;
};
struct Struct {
Struct() {}
int x;
};
int main()
{
Class* c = new Class();
printf("%i\n", c->x);
Struct* s = new Struct();
printf("%i\n", s->x);
}
|
Compile and run it:
| C:\test>g++ test.cpp -o test
C:\test>test.exe
7349392
7234128
|
Btw this has nothing to do with "new", same thing will happen when you place objects on stack:
1
2
3
4
5
6
7
8
9
10
11
12 | int main()
{
// assume Struct and Class are without explicit constructor, like in examples nr1 and nr2 above
Class c1{}; // cannot use () because compile will think this is function declaration
Class c2;
Struct s1{};
Struct s2;
// now c1.x and s1.x will be 0
// but c2.x and s2.x will contain garbage
}
|