编号 | 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文件中。
以上为我自己的实验结果,缺乏理论分析,大家可以自己思考下。