zoukankan      html  css  js  c++  java
  • 3.3内联函数

    inline只是对于编译器的建议

    一般情况下,对内联函数有如下的限制:

    1不能有递归

    2不能包含静态数据

    3不能包含循环

    4不能包含switch和goto语句

    5不能包含数组

    若一个内联函数定义不满足以上限制,则编译器会把它当作普通函数

    使用关键字inline说明的函数称内联函数。在C++中,除具有循环语句、switch语句的函数不能说明为内联函数外,其他函数都可以说明为内联函数。

     1 #include <iostream>
     2 using namespace std;
     3 
     4 inline int getX3(int x);//声明内联函数
     5 
     6 inline int getX3(int x)//定义
     7 {
     8     return x*x*x;
     9 }
    10 
    11 void main()
    12 {
    13     std::cout << getX3(1 + 2) << std::endl;//27
    14 
    15     system("pause");
    16 }

    //内联函数原则上放在头文件

    //内联函数需要展开,VS要求放在头文件

    //如果一个类在头文件定义,而这个类里面有内联函数,则这个内联函数也要在头文件定义。

    //如果内联函数在头文件声明,在源文件定义,将会报错

    //error LNK2001: 无法解析的外部符号 "public: void __thiscall fushu::showall(int,int)" (?showall@fushu@@QAEXHH@Z)

    在头文件定义一个类:

     1 class fushu
     2 {
     3 public:
     4     fushu();
     5     ~fushu();
     6 
     7     //内联函数原则上放在头文件
     8     //内联函数需要展开,VS要求放在头文件
     9     //如果一个类在头文件定义,而这个类里面有内联函数,则这个内联函数也要在头文件定义。
    10     //如果内联函数在头文件声明,在源文件定义,将会报错
    11     //error LNK2001: 无法解析的外部符号 "public: void __thiscall fushu::showall(int,int)" (?showall@fushu@@QAEXHH@Z)
    12     inline void fushu::showall(int, int);
    13 };
    14 
    15 inline void fushu::showall(int x, int y)
    16 {
    17     //复合代码
    18     std::cout << (this->x = x) << " " << (this->y = y) << std::endl;
    19 }
  • 相关阅读:
    随笔2
    随笔
    关于updateElement接口
    随笔1
    本地访问正常,服务器访问乱码 记录
    Redis (error) NOAUTH Authentication required.解决方法
    tomcat启动很慢 停留在 At least one JAR was scanned for TLDs yet contained no TLDs.
    微信公众号消息回复
    微信公众号 报token验证失败
    idea中web.xml报错 Servlet should have a mapping
  • 原文地址:https://www.cnblogs.com/denggelin/p/5648653.html
Copyright © 2011-2022 走看看