今天復習C++ Primer的時候,看到了關于C++類的內聯成員函數的放置,應該放在頭文件中。那么這到底是為什么
呢?僅僅是一種代碼規范問題還是必須這樣做呢?
下面我就來講講我自己的理解吧。要徹底理解這個問題,首先就要了解下函數的聲明和定義了。我們知道,函數可以
在多處聲明,但只能在一個地方定義,不然就會出現重定義。大部分函數默認是外部鏈接,而inline函數默認為內部鏈
接。也就是說inline函數只能在本文件中使用,對其他文件是不可見的。一般我們使用某個類的時候,都是在文件中加
上該類的頭文件,以便我們可以使用該類的接口。而我們類的成員函數的實現都是放在相應的.cpp文件中的,而在.h
文件中聲明。這樣我們便可以通過.h文件中的成員函數的聲明找到其定義,繼而使用成員函數了。但如果將inline函數
放在.cpp文件中,那么其只對.cpp文件有效,這樣我們就無法訪問它了。所以我們將其放在類的聲明的頭文件中,這
樣通過包含該頭文件來使用它。
下面寫個實際的例子來說明一下,我先把內聯函數放到類聲明的頭文件中:
/*test.h*/
#ifndef TEST_H
#define TEST_H#include <iostream>
using std::cout;
using std::endl;class test
{
public:test():x(10){}inline void print();void display (int y);
private:int x;
};void test::print()
{cout << x << endl;
}#endif
/*test.cpp*/
#include <iostream>
#include "test.h"
using std::cout;
using std::endl;void test::display(int y)
{cout << x * y << endl;
}
運行結果正常,下面來看看將內聯函數放到.cpp中去:
/*test.h*/
#ifndef TEST_H
#define TEST_H#include <iostream>
using std::cout;
using std::endl;class test
{
public:test():x(10){}inline void print();void display (int y);
private:int x;
};#endif
/*test.cpp*/
#include <iostream>
#include "test.h"
using std::cout;
using std::endl;void test::print()
{cout << x << endl;
}void test::display(int y)
{cout << x * y << endl;
}
測試函數和上面的main.cpp是一樣的。這是出現了錯誤:
error LNK2019: 無法解析的外部符號 "public: void __thiscall?test::print(void)" (?print@test@@QAEXXZ),該符號在函
數 _main 中被引用。如果我將測試函數改為:
int main()
{test T;T.display(10);//T.print();system("pause");return 0;
}
那么運行結果正常。從此可以得出結論:內聯函數放在頭文件或者.cpp中都是沒有錯的,但如果我們需要在程序中訪
問它,那么就必須將其放在頭文件中。
C++類的內聯成員函數應放在哪
》