Test the new object in C ++

Source: Internet
Author: User

 

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
  1. Int * a = new;
  2. 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?
  1. Class param
  2. {
  3. Private:
  4. Int x, y;
  5. }
  6. Void main ()
  7. {
  8. Param * a = new Param;
  9. Param * B = new Param ();
  10. Param c;
  11. Param d;
  12. Param * e = new Param ();
  13. }

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
  1. Int * a = new;
  2. 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?
  1. Class Param
  2. {
  3. PRIVATE:
  4. Int X, Y;
  5. }
  6. Void main ()
  7. {
  8. Param * a = new Param;
  9. Param * B = new Param ();
  10. Param c;
  11. Param d;
  12. Param * e = new Param ();
  13. }

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.