zoukankan      html  css  js  c++  java
  • 内联成员函数应放在哪

    今天复习C++ Primer的时候,看到了关于C++类的内联成员函数的放置,应该放在头文件中。那么这到底是为什么

    呢?仅仅是一种代码规范问题还是必须这样做呢?

    下面我就来讲讲我自己的理解吧。要彻底理解这个问题,首先就要了解下函数的声明和定义了。我们知道,函数可以

    在多处声明,但只能在一个地方定义,不然就会出现重定义。大部分函数默认是外部链接,而inline函数默认为内部链

    接。也就是说inline函数只能在本文件中使用,对其他文件是不可见的。一般我们使用某个类的时候,都是在文件中加

    上该类的头文件,以便我们可以使用该类的接口。而我们类的成员函数的实现都是放在相应的.cpp文件中的,而在.h

    文件中声明。这样我们便可以通过.h文件中的成员函数的声明找到其定义,继而使用成员函数了。但如果将inline函数

    放在.cpp文件中,那么其只对.cpp文件有效,这样我们就无法访问它了。所以我们将其放在类的声明的头文件中,这

    样通过包含该头文件来使用它。

    下面写个实际的例子来说明一下,我先把内联函数放到类声明的头文件中:

    1. /*test.h*/  
    2. #ifndef TEST_H  
    3. #define TEST_H  
    4.   
    5. #include <iostream>  
    6. using std::cout;  
    7. using std::endl;  
    8.   
    9. class test  
    10. {  
    11. public:  
    12.     test():x(10){}  
    13.     inline void print();  
    14.     void display (int y);  
    15. private:  
    16.     int x;  
    17. };  
    18.   
    19. void test::print()  
    20. {  
    21.     cout << x << endl;  
    22. }  
    23.   
    24. #endif  
    1. /*test.cpp*/  
    2. #include <iostream>  
    3. #include "test.h"  
    4. using std::cout;  
    5. using std::endl;  
    6.   
    7. void test::display(int y)  
    8. {  
    9.     cout << x * y << endl;  
    10. }  
    1. /*main.cpp*/  
    2. #include <iostream>  
    3. #include "test.h"  
    4. using namespace std;  
    5.   
    6. int main()  
    7. {  
    8.     test T;  
    9.     T.display(10);  
    10.     T.print();  
    11.   
    12.     system("pause");  
    13.     return 0;  
    14. }  


    运行结果正常,下面来看看将内联函数放到.cpp中去:

    1. /*test.h*/  
    2. #ifndef TEST_H  
    3. #define TEST_H  
    4.   
    5. #include <iostream>  
    6. using std::cout;  
    7. using std::endl;  
    8.   
    9. class test  
    10. {  
    11. public:  
    12.     test():x(10){}  
    13.     inline void print();  
    14.     void display (int y);  
    15. private:  
    16.     int x;  
    17. };  
    18.   
    19. #endif  
    1. /*test.cpp*/  
    2. #include <iostream>  
    3. #include "test.h"  
    4. using std::cout;  
    5. using std::endl;  
    6.   
    7. void test::print()  
    8. {  
    9.     cout << x << endl;  
    10. }  
    11.   
    12. void test::display(int y)  
    13. {  
    14.     cout << x * y << endl;  
    15. }  


    测试函数和上面的main.cpp是一样的。这是出现了错误:

    error LNK2019: 无法解析的外部符号 "public: void __thiscall test::print(void)" (?print@test@@QAEXXZ),该符号在函

    数 _main 中被引用。如果我将测试函数改为:

    1. int main()  
    2. {  
    3.     test T;  
    4.     T.display(10);  
    5.     //T.print();  
    6.   
    7.     system("pause");  
    8.     return 0;  
    9. }  


    那么运行结果正常。从此可以得出结论:内联函数放在头文件或者.cpp中都是没有错的,但如果我们需要在程序中访

    问它,那么就必须将其放在头文件中。

  • 相关阅读:
    vscode webstrom 配置 eslint 使用 airbnb 规范
    IntelliJ idea webstrom Visual Studio Code vscode 设置cmder为默认终端 Terminal
    intellij idea 大内存优化配置 idea64.exe.vmoptions文件配置
    解决因 gtx 显卡而导致的 google chrome 颜色显示不正常。色彩变淡发白,其实很简单
    href=http:// href=// 的区别,src=http:// src=// 的区别。 链接里不带http,链接里直接使用双斜线 // 有什么不同。http://和//有什么区别?
    前后端分离之fiddler前端开发代理 autoresponder 正则表达式 regex:(?insx) 修正符详解
    开发环境部署脚手架搭建 步骤
    开发环境部署git 步骤
    requestAnimationFrame移动端实现回到顶部效果
    占位图片
  • 原文地址:https://www.cnblogs.com/hwl1023/p/4921225.html
Copyright © 2011-2022 走看看