class base
{
public:
virtual void somefunction();
}
class derived : public base
{
public:
void somefunction();
unsigned int variable;
}
C++: sizeof(class with virtual functions)
Hi
if I have a class derived from a abstract base class in C++
like this
Does sizeof(derived) return 4 bytes for unsigned int variable or 4 bytes for uint.. plus the size of the vtable pointer?
You can't rely on the size of a structure being the same between architectures, compilers, or even on the same compiler and architecture with different compile options.
That's why sizeof() exists, because the size of a structure is decided by the compiler at compile time.
Mark
That's why sizeof() exists, because the size of a structure is decided by the compiler at compile time.
Mark
Quote:Original post by Basiror
so the table pointer is included
thx alot
Yes, it's included. The C++ compiler basically introduces a hidden member for your class that contains the pointer to the vtable (depends much on how the compiler implements virtual functions).
Now that you know the answer, I wonder why you want to know? There isn't much useful information that can be gotten out of the knowledge.
CM
CM
The nitpicky would tell you that how exactly virtual function lookup works is implementation-dependent and might not be based on vptrs at all.
Even for compilers that do use them once you throw multiple inheritance in the mix you can easily end up with multiple vptrs.
i.e. any use you make of the information coming out of this thread is compiler-dependent and could change even between versions of the same compiler.
Even for compilers that do use them once you throw multiple inheritance in the mix you can easily end up with multiple vptrs.
i.e. any use you make of the information coming out of this thread is compiler-dependent and could change even between versions of the same compiler.
You may be interested to read about empty base class optimisation as it applies to your code example.
Quote:Original post by Conner McCloud
Now that you know the answer, I wonder why you want to know? There isn't much useful information that can be gotten out of the knowledge.
CM
Actually, understanding that the compiler tacks on the hidden member is very useful, especially if you can find out how much is tacked on. This is important if you want to, for example, dump the entire class out to file as raw data (not serialized) and then hope to reconstruct the vftable when you reload it. Which is not something I recommend doing, btw ;)
Heck, anytime you're very concerned about space information like this is very useful =) In certain cases, knowing where the vftable member is placed in the object could also be extremely important :)
Just my 2c :)
Quote:Original post by Grafalgar
Actually, understanding that the compiler tacks on the hidden member is very useful, especially if you can find out how much is tacked on. This is important if you want to, for example, dump the entire class out to file as raw data (not serialized) and then hope to reconstruct the vftable when you reload it.
When would you want to do this?
Quote:Original post by Grafalgar
Which is not something I recommend doing, btw ;)
Then how important can it be?
Quote:Original post by Grafalgar
Heck, anytime you're very concerned about space information like this is very useful =) In certain cases, knowing where the vftable member is placed in the object could also be extremely important :)
Why would you want to do this?
When answering these questions, keep in mind the original poster is clearly a begginer who isn't ready for the sort of advanced topics such knowledge is useful for. If he were, he would have known how to answer that question himself.
CM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement