The short answer: It will be stored in the text or code section of the binary only once irrespective of the number of instances of the class created.
The functions are not stored separately anywhere for each instance of a class. They are treated the same way as any other non-member function would be. The only difference is that the compiler actually adds an extra parameter to the function,which is a pointer of the class type.
For example the compiler will generate the function prototype like this:
void func(Foo* this);
(Note that this may not be the final signature. The final signature can be much more cryptic depending on various factors including the compiler)
Any reference to a member variable will be replaced by
this-><member> //for your ex. a=0 translates to this->a = 0;
So the line foo->func(); roughly translates to:
- Push the value of Foo* on to the stack. #Compiler dependent
- call func which will cause the instruction pointer to jump to the offset of func in the executable #architecture dependent Read this and this
- Func will pop the value from stack. Any further reference to a member variable would be preceded by dereferencing of this value
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…