How to get the size of a non-static member of a class without construct that class?
C
When applied to an expression, sizeof does not evaluate the expression...
Hence, we can write:
sizeof(((Foo*) 0)->m);
C++
c++0x allow that:
// a more elegant way
sizeof(Foo::m);
A nested-name-specifier that denotes a class, optionally followed by the keyword template ([temp.names]), and then followed by the name of a member of either that class ([class.mem]) or one of its base classes, is a qualified-id; ... The result is an lvalue if the member is a static member function or a data member and a prvalue otherwise.
Reference
Extending sizeof to apply to non-static data members without an object (revision 1)
About the author
後端工程師。 不定時張貼一些寫扣時的筆記,有時英文,有時中文。 「如果,我是魔女的話...」
Comments
Join the discussionNo comments yet. Be the first to comment.
