How the member functions of a C + + class are stored (whether they belong to the class object)---thinking about the questions raised by the question

Source: Internet
Author: User

Yesterday to interview a company, the interview problem has a problem, I did not clear, first recorded as follows:

class d{public:    void  PrintA ()    {        cout<< PrintA "<<Endl;    }     Virtual void PRINTB ()    {        cout<<"printb"<<Endl;    }};


Main function Call:

D *d=null;d,PrintA ();d->printb ();

What is the output?

It was thought that object D was directly initialized to NULL, that the non-virtual member function did not have an address, and that the virtual member function, since the object would have pointers to the virtual function table,-vptr, a dummy function table that points to the VM list, should be able to fetch the address ( actually , the printb of this virtual function is most expected to be a direct crash, because D points to null, that is, the address is 0x00000000, and then to find the virtual address pointer, is certainly not allowed .

Here's a concrete analysis.

Let's take a look at the member functions of the class.

Classes A, B, c three classes, one is nothing really empty class, one is a class with member functions, and the last is a class with a virtual function.

class a{}; class b{public:    B () {}    ~B () {}}; class c{public:    C () {}    virtual ~C () {}};

So what's the size of their memory?

cout<<"a="<<sizeof(A) <<endl;cout<<"  b="<<sizeof(B) <<endl;cout<<"c=  "<<sizeof(C) <<endl;

Test results on 32-bit Windows XP machines:

A=1

B=1

C=4

The comparison of A and B shows that the member function is non-occupying class space, and then a specific example:

class e{public:    e () {}    ~e () {}private:    int  m_data;     Char c;};


sizeof (E) outputs the result on a 32-bit machine, if the alignment is not considered to be 5, the consideration is 8, and the expected consistency of the above class B is visible.

We can say that static data members and static member functions are part of the class, not part of the object (as Mr. Tam said).

When we instantiate an object, because the object is defined by a class, it takes the data and functions of this class for granted. However, in general, different objects, their data values are different, but the code for the function is the same. So, in order to conserve storage space (imagine that if we define 100 objects, it would be wasteful to store the same code in a 100-segment memory space.) ), we let the member functions of the Code share.

We store the code of the member function outside of the object space. In other words, the code of the member function does not occupy the storage space of the object. It will be there somewhere else.

So the member function of the class, for the class. On the one hand is the logical "belong", on the one hand is the physical "not dependent".

Back to study questions, for non-static member functions, it is, of course, part of the object. (Just because of the particularity of the storage way, it is easy to misunderstand!) )

Answer the opening question:

Class includes member variables and member functions
New is just a member variable, the member function always exists
So if the member function does not use any member variable, it will work, whether it is static or not.

Space of an object = size of all member variables

If the object's class has a virtual function, there may be more pointers to the virtual table

All functions are stored in an object-independent storage space
When an object calls a function, there is no problem with calling the static member function directly, and the member function needs to pass itself to the function with the this pointer to indicate which object to invoke

So call a static member function with an uninitialized pointer, or call a member function that does not use any member variable (that is, the this pointer is not used)

Theoretically it is feasible, as for the specific branch does not support the look at each compiler bar

Thinking:

In the main function, if

D D;//=null;
D.printa ();
D.PRINTB ();

Call it?

Can output normally, D on the stack ...

(go) How the member functions of a C + + class are stored (whether they belong to the class object)---thinking about the questions raised by the question

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.