When defining a member function out-of-line, which qualifiers must appear in the declaration / definition / both?

后端 未结 3 751
别那么骄傲
别那么骄傲 2021-01-20 23:50

I am almost sure this has been asked before. Unfortunately, my C++ has become so rusty that I don\'t even know what to search for.

Is there an easy-to-remember rule of t

3条回答
  •  清酒与你
    2021-01-21 00:25

    The best way to know which modifiers (or "specifiers") have to be used in which instance is to understand what each one of them means, and what they do.

    const is a part of the method's "signature". A signature consists of what the function returns, the types of its parameters, and whether it is a constant method (the const part). That cannot be changed, and must remain as is.

    Everything else, virtual, and override in your list, is related to the method's declaration. It's not a part of the method's signature, and can only appear in the method's declaration.

    The only rule of thumb, if there is one, is that anything that's a part of the method's signature must be unchanged, when the method is not defined inline. And if it's not, it must be a part of the declaration, only (but, as with every rule of thumb, there's always an exception, the inline keyword).

    Note that default parameter values are not considered to be a part of the method's signature. Default parameter values must be specified in the declaration only. But, if the method is defined inline, the default parameter values wind up as part of the method's definition!

提交回复
热议问题