How does sizeof work? How can I write my own?

后端 未结 10 2326
粉色の甜心
粉色の甜心 2021-02-08 07:29

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

10条回答
  •  北海茫月
    2021-02-08 08:20

    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!

提交回复
热议问题