Question : In which memory a class gets stored?
Answer : A class is nothing but data type description. Which is present in the code. So I don't think the class will have any memory allocated to it.
As far as object is concerned, the object will be stored in stack if it is a local object, in heap if it is dynamic object and in data segment if it is a static object.
How are the methods i.e. function members of the object stored. They are NOT stored as objects have pointers to actual function. In fact there is a single function per class in the code segment. In that situation how does the function access the data members of the object. This is achieved with a special pointer this which gets passed to the function.
A static member function is special because it does not get this pointer. So it CAN NOT access the non-static data of the object.
And a virtual function is handled in another way. Here every object which has atleast one virtual function will have a special pointer vptr which points to vtbl a per class table which holds the address of all virtual functions.
Read a better description of all this in
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=195
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment