简体   繁体   English

如果在结构体外定义C ++结构成员函数定义有差异吗?

[英]C++ struct member functions definitions have differences if they are defined outside the struct body ?

A C++ question about struct member functions. 关于struct成员函数的C ++问题。

What is the difference between f1() and f2() except their names ? 除了名字之外, f1()f2()什么区别?

 struct F{
   int f1(){
      return 0;  
    }
    int f2();    
 }; 
 int F::f2(){
     return 0;
 }

May I say f1() is inline but f2() is not ? 我可以说f1()是内联的但是f2()不是吗?

You are correct that f1 is inline and f2 is not, but its not just because it was defined inside the class. 你是正确的, f1是内联的,而f2不是,但它不仅仅是因为它是在类中定义的。 f2 could also be inline if it was defined as 如果定义为f2也可以是内联的

inline int F::f2() {
    return 0;
}

The C++11 spec section 9.3 says that f1 is "defined in its class definition" and f2 is "defined outside of its class definition." C ++ 11规范第9.3节说f1是“在其类定义中定义的”,而f2是“在其类定义之外定义的”。 It then states that any function defined inside its class definition is inline, while functions defined outside of its class definition must be explicitly marked as inline (like in my example above), or else they are non-inline (like your f2 ). 然后它声明在其类定义中定义的任何函数都是内联的,而在其类定义之外定义的函数必须明确标记为内联(如上例中所示),否则它们是非内联函数(如f2 )。

Inside the class definition vs. outside the definition is unimportant, other than making functions implicitly inline. 除了使函数隐式内联之外,在类定义内部与定义之外的定义并不重要。 The concept of inside a class definition and outside a class definition only appears in 9.3.2-9.3.5, while the broader concept of "inline" appears in other parts of the spec. 类定义内部和类定义之外的概念仅出现在9.3.2-9.3.5中,而“内联”的更广泛概念出现在规范的其他部分中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM