I know C++ and know the function sizeof
itself but I need to write my own sizeof function so please explain how it works exactly? What does it do with the parameter
As already said it is an operator not a function, but additionally it is one of the operators for which operator overloading is not allowed:
Bjarne Stroustrup's C++ Style and Technique FAQ: Why can't I overload dot, ::, sizeof, etc.?
I can think of no conceivable reason why you would want to overload this in any case. If you have an class for which size information other than that which sizeof
yields is required, then simply add a member function to provide that information; as for example in std::string:size()
which returns the length of the string managed by the object rather than the size of the object which is semantically different; you do not want to monkey with the semantics of sizeof
!