C++編寫Lib工程和Exe工程時,發生class或函數重名後的決策過程

來源:互聯網
上載者:User

編號

Lib

Exe

1

 

bool glbFunc(Cls&){return true;}

bool glbFunc(Cls&){return false;},不可

inline bool glbFunc(Cls&){return false;},不可

2

inline bool glbFunc(Cls&){return true;}

bool glbFunc(Cls&){return false;},可以

inline bool glbFunc(Cls&){return false;},可以

3

class Cls{

public:

void clsFunc(){}

};

class Cls{

public:

void clsFunc(){}

};,可以

class Cls{

public:

void clsFunc();

};

void Cls::clsFunc(){},可以

4

class Cls{

public:

void clsFunc();

};

void Cls::clsFunc(){}

class Cls{

public:

void clsFunc(){}

};,可以

class Cls{

public:

void clsFunc();

};

void Cls::clsFunc(){},不可

5

class Cls{

public:

static const int i=1;

};

class Cls{

public:

static const int i=2;

};,可以

class Cls{

public:

static const int i;

};

const int Cls::i=2;,不可

6

class Cls{

public:

static const int i;

};

const int Cls::i=1

class Cls{

public:

static const int i=2;

};,不可

class Cls{

public:

static const int i;

};

const int Cls::i=2;,不可


補充:

1、 如上表編號1所示,非內聯全域函數glbFunc使用了class Cls,如果Exe工程中重新定義了glbFunc,則編譯報錯,因為glbFunc已經在Lib工程中被定義了一次。但是,如果在Exe工程中重新定義glbFunc的同時,重新定義了class Cls,則編譯通過,此時使用Exe工程中的glbFunc和class Cls。

2、 存在以下情形:

Lib工程中使用編號2的方式定義內聯全域函數glbFunc,使用編號4的方式定義class Cls的clsFunc函數;Exe工程中重新定義全域函數glbFunc(內聯或非內聯均可),那麼,在Exe工程中調用全域函數glbFunc,將調用Exe工程中定義的全域函數glbFunc,該函數中使用的class Cls,將使用Lib工程中定義的class Cls!這樣很容易出現問題,所以,在產生Lib工程時,應慎用內嵌函式,同時,不要將函數實現放入標頭檔(.h檔案)中,而應該將實現全部放入.cpp檔案中。

以上為我自己的實驗結果,缺乏理論分析,大家可以自己思考下。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.