zoukankan      html  css  js  c++  java
  • C++中的内联成员函数与非内联成员函数

    在C++中内联成员函数与非内联成员函数的可以分为两种情况:

    1.如果成员函数的声明和定义是在一起的,那么无论有没有写inline这个成员函数都是内联的,如下:

    using namespace std;
    class test{
    public:
    	void fuc() {
    		cout << "ok!" << endl;
    	}
    };
    int main(void)
    {
    	test t, t1;
    	t.fuc();
    	t1.fuc();
    
    	return 0;
    }
    

      或者:

    using namespace std;
    class test{
    public:
    	inline void fuc() {
    		cout << "ok!" << endl;
    	}
    };
    int main(void)
    {
    	test t, t1;
    	t.fuc();
    	t1.fuc();
    
    	return 0;
    }
    

    2.如果成员函数的声明和定义是分开的,那么如果两者中有一个加上了inline都会使成员函数都是内联的,如:

    #include <iostream>
    using namespace std;
    class test{
    public:
    	inline void fuc();
    };
    int main(void)
    {
    	test t, t1;
    	t.fuc();
    	t1.fuc();
    
    	return 0;
    }
    void test::fuc(){
    	cout << "ok!" << endl;
    }
    

     或:

    #include <iostream>
    using namespace std;
    class test{
    public:
    	void fuc();
    };
    int main(void)
    {
    	test t, t1;
    	t.fuc();
    	t1.fuc();
    
    	return 0;
    }
    inline void test::fuc(){
    	cout << "ok!" << endl;
    }
    

    要想定义非内联成员函数,只有一种方法即:声明和定义都不加inline,如下

    #include <iostream>
    using namespace std;
    class test{
    public:
    	void fuc();
    };
    int main(void)
    {
    	test t, t1;
    	t.fuc();
    	t1.fuc();
    
    	return 0;
    }
    void test::fuc(){
    	cout << "ok!" << endl;
    }
    

    ------------------------------------------------------------------------------------------------------------------------------

    验证时使用Release版本编译同时在链接选项进行如下配置:

     之后可以查看编译后的汇编代码之间的区别来验证。

  • 相关阅读:
    🔨FFmpeg 转换至 MP4 视频格式
    🔨Ubuntu Linux '门' '复' 显示不标准
    🔨Youtube-dl 开代理搭配 Aria2 多线程加速下载
    📔 如何用英语的思维来思考 How to THINK in English
    🚀 sublime 加速软件下载
    🚀 snap 代理
    🚀 Chocolatey 代理
    🔨 Deepin V20 软件及遇到的问题
    💿 npm 换源 (转载)
    🔨FFmpeg 合并视频
  • 原文地址:https://www.cnblogs.com/xxNote/p/4082838.html
Copyright © 2011-2022 走看看