Of course, all the method members take space in a per-class manner, not per-object - the code of a single method of a single class is kept in memory only once, regardless of the number of objects.
Be careful with virtual functions though... using them adds additional memory to your class because individual copies of your class must then keep track of which function is correct to call. |
A bit unclear explanation. What are "individual copies of the class"? You meant instances? If instances, then you are wrong.
The first virtual method adds usually a single pointer to the object, the next virtual methods are for free. So you should be careful, when you keep extremely large amounts of really small objects (like 4-16 bytes). Then, the 4 or 8 bytes overhead might be a problem. But for large objects, that single pointer won't make a noticeable difference.
What virtual adds to your class definitions is a virtual method table - there is only one per class, and it contains pointers to all the virtual methods in that class. Usually a few pointers in the vmt take relatively little space compared to the actual code defined in the class. So, again, in most cases, you won't probably even notice.
The more serious downside of virtual calls in C++ is that in most cases they can't be inlined.
So calling them may incur some significant time overhead, compared to statically resolved, inlined calls. Therefore, you have to think if you really want the runtime polymorphism, or maybe the (weaker) compile time polymorphism (templates) suffice.