Memory allocation for member functions in C++ -


#include<iostream> using namespace std; class {  }; class b {         public:                 void disp()                 {                         cout<<" not virtual function.";                 } }; class c {         public:                 virtual void disp()                 {                         cout<<"this virtual function.";                 } }; int main() {         cout<<"class a"<<sizeof(a)<<endl;         cout<<"class b"<<sizeof(b)<<endl;         cout<<"class c"<<sizeof(c)<<endl;         return 0; } 

sizeof class , class b both 1 byte only.what memory allocation member function disp in b.

for each instance of class, memory allocated member variables i.e. each instance of class doesn't it's own copy of member function. instances share same member function code. can imagine compiler passing hidden this pointer each member function operates on correct object. in case, since c++ standard explictly prohibits 0 sized objects, class , class b have minimum possible size of 1. in case of class c since there virtual function each instance of class c have pointer v-table (this compiler specific though). sizeof class sizeof(pointer).


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -