C + + class issues

Source: Internet
Author: User


1. In C + + hollow structures and classes are 1 bytes, this 1 bytes is a special processing of the compiler. Otherwise, we'll consider the following procedure.


A;

A b;


cout<<&a<<endl;

cout<<&b<<endl;


Assuming that the empty struct and the empty class do not account for memory, then object A and object B must have the same address, and different objects have the same address, which is a very unreasonable logic.


So the compiler makes a special deal, classes and structs occupy at least one byte,


The other size of the class with the virtual function is at least 4 because the vptr is a pointer that takes up to 4 bytes.


  
 
  1. classA
  2. {
  3. private:
  4.        int m_value;
  5.  
  6. public:
  7.         A(intvalue)
  8.         {
  9.                 m_value=value;
  10.         }
  11.        void Print1()
  12.         {
  13.                 printf("hello world");
  14.         }
  15.        void Print2()
  16.         {
  17.                 printf("%d", m_value);
  18.         }
  19. };
  20.  
  21. int main()
  22. {
  23.         A*pA=NULL;
  24.         pA->Print1();
  25.         pA->Print2();
  26.  
  27.        return 0;
  28. }

2. This question examines the candidate's mastery of the C + + object model, in C + +, where member variables and member functions defined in a class are stored separately, all member functions are stored in code snippets, and member variables are stored on stacks or heaps.

Once you understand this object model, you know that member variables are not accessed in print1, so that you do not have access to the 0 address area reserved by the system, so there is no error, and the Print2 access member variable is accessed by the object model, which is the 0 address area, so the segment is wrong.


  
 
  1. classA
  2. {
  3. private:
  4.        int m_value;
  5.  
  6. public:
  7.         A(intvalue)
  8.         {
  9.                 m_value=value;
  10.         }
  11.        void Print1()
  12.         {
  13.                 printf("hello world");
  14.         }
  15.        virtual void Print2()
  16.         {
  17.                 printf("hello world");
  18.         }
  19. };
  20. int main()
  21. {
  22.         A*pA=NULL;
  23.         pA->Print1();
  24.         pA->Print2();
  25.  
  26.        return 0;
  27. }


3. There is no valid object at the null address, so there is no valid vptr, so there will be a segment error when calling Print2, because according to the virtual function, the 4 bytes starting with NULL are not likely to point to the virtual function table. function calls made through these 4 bytes are only dead end.




From for notes (Wiz)

C + + class issues

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.