Reading section 5.11 new and delete of version 4 of C ++ Primer creates some doubts. For a class param that does not define any constructor,
New param () and new param are different, and the same is true for built-in types. The example in the book only gives the built-in type int
View plaincopy
To clipboardprint? ··· · 50 ······· · 90 ····· · 140 · 150
- Int * a = new;
- Int * B = new B ();
Int * a = new a; <br/> int * B = new B ();
Where a is a random value, according to the statement in the book, a is the value of the last time in the memory, B is initialized to 0; the result of the current test class type
View plaincopy
To clipboardprint?
- Class param
- {
- Private:
- Int x, y;
- }
- Void main ()
- {
- Param * a = new Param;
- Param * B = new Param ();
- Param c;
- Param d;
- Param * e = new Param ();
- }
Class Param <br/>{< br/> PRIVATE: <br/> int X, Y; <br/>}< br/> void main () <br/>{< br/> param * A = new Param; <br/> param * B = new param (); <br/> param C; <br/> param D; <br/> param * E = new param (); </P> <p>}
This is the value before debugging
This is the value after debugging.
From this we can see that for classes that do not provide any constructor, adding parentheses when new will help you create a default constructor and help you initialize it;
When new is used, no brackets are added, and a default constructor will be constructed for you, but nothing is done.
Reading section 5.11 new and delete of version 4 of C ++ primer creates some doubts. For a class Param that does not define any constructor,
New param () and new Param are different, and the same is true for built-in types. The example in the book only gives the built-in type int
View plaincopy
To clipboardprint? ··· · 50 ······· · 90 ····· · 140 · 150
- Int * a = new;
- Int * B = new B ();
Int * a = new a; <br/> int * B = new B ();
Where a is a random value, according to the statement in the book, A is the value of the last time in the memory, B is initialized to 0; the result of the current test class type
View plaincopy
To clipboardprint?
- Class Param
- {
- PRIVATE:
- Int X, Y;
- }
- Void main ()
- {
- Param * a = new Param;
- Param * B = new Param ();
- Param c;
- Param d;
- Param * e = new Param ();
- }
Class param <br/>{< br/> private: <br/> int x, y; <br/>}< br/> void main () <br/>{< br/> Param * a = new Param; <br/> Param * B = new Param (); <br/> Param c; <br/> Param d; <br/> Param * e = new Param (); </p> <p>}
This is the value before debugging
This is the value after debugging.
From this we can see that for classes that do not provide any constructor, adding parentheses when new will help you create a default constructor and help you initialize it;
When new is used, no brackets are added, and a default constructor will be constructed for you, but nothing is done.