-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
192 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
project(CheckLinkage) | ||
|
||
set(CMAKE_CXX_STANDARD 17) | ||
|
||
add_executable(main main.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
static int internalVar = 42; | ||
int Var = 34; | ||
|
||
void internalFunc() { | ||
static int localStatic = 0; | ||
} | ||
|
||
class MyClass{ // 本质上此时MyClass只是声明 | ||
public: | ||
int ClassVar = 23; | ||
static int ClassStatic; // 在此只是声明 | ||
void printVar(); | ||
}; | ||
int MyClass::ClassStatic = 10; //外部链接 | ||
void MyClass::printVar(){ | ||
static int b = 33; | ||
int a = 23; | ||
} | ||
|
||
template<typename T> T th(T a, T b){ | ||
return a + b; | ||
} | ||
|
||
static MyClass mc; | ||
MyClass mac; | ||
|
||
|
||
template<class T2,class T3> // 模板函数 --(1) | ||
int h(T2 a,T3 b){ // 返回值中有模板形参 | ||
return a+b; | ||
} | ||
|
||
int h(int a, int b){ // 普通函数 --(2) | ||
return a+b; | ||
} | ||
|
||
template<> int h(float a, float b){ // 对函数特化 --(3) | ||
return a+b; | ||
} | ||
|
||
template<typename T> | ||
class TypeClass{ | ||
public: | ||
T kk; | ||
int ik = 789; | ||
static T staticKK; | ||
}; | ||
|
||
template class TypeClass<int>; // 实例化声明,会实例化模板 | ||
|
||
TypeClass<int> tc; | ||
|
||
template<typename T> | ||
static T TypeClass<T>::staticKK; // 泛型定义 | ||
|
||
template<> int TypeClass<int>::staticKK = 10; | ||
|
||
int main() { | ||
// internalFunc(); | ||
MyClass mcc; | ||
th<int>(2,3); | ||
th<double>(2.0,3.0); | ||
h(2.3f,3.6f); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
static int internalVar = 42; | ||
int Var = 34; | ||
|
||
int main(){ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// 普通函数 | ||
void interFunc(){ | ||
static int localStatic = 22; | ||
} | ||
|
||
// 模板函数 | ||
template<class T2, class T3> // 模板函数 --(1) | ||
int h(T2 a,T3 b){ // 返回值中有模板形参 | ||
return a+b; | ||
} | ||
|
||
int h(int a, int b){ // 普通函数 --(2) | ||
return a+b; | ||
} | ||
|
||
template<> int h(float a, float b){ // 对模板函数特化 --(3) | ||
return a+b; | ||
} | ||
|
||
int main(){ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 普通函数 | ||
void interFunc(){ | ||
static int localStatic = 22; | ||
} | ||
|
||
// 模板函数 | ||
template<class T2, class T3> // 模板函数 --(1) | ||
int h(T2 a,T3 b){ // 返回值中有模板形参 | ||
return a+b; | ||
} | ||
|
||
int h(int a, int b){ // 普通函数 --(2) | ||
return a+b; | ||
} | ||
|
||
template<> int h(float a, float b){ // 对模板函数特化 --(3) | ||
return a+b; | ||
} | ||
|
||
template int h(char, char); // 实例化声明 | ||
|
||
int main(){ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class MyClass{ // 本质上此时MyClass只是声明 | ||
public: | ||
int ClassVar = 23; //定义,但是依赖于class对象(无链接) | ||
static int ClassStatic; // 在此只是声明(不会定义) | ||
void printVar(); // 声明不定义 | ||
void inlineFunc(){ // 非静态函数 类内定义 | ||
static int kk = 33; | ||
} | ||
static void staticFunc1(){} // 静态函数 类内定义 | ||
static void staticFunc2(); | ||
}; | ||
int MyClass::ClassStatic = 10; // 静态变量 类外定义 | ||
void MyClass::printVar(){ // 非静态函数 类外定义 | ||
static int b = 33; | ||
int a = 23; | ||
} | ||
void MyClass::staticFunc2(){} // 静态函数 类外定义 | ||
|
||
MyClass mc; | ||
|
||
int main(){ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
template<typename T> | ||
class TypeClass{ | ||
public: | ||
T kk; | ||
int ik = 789; | ||
static T staticKK; | ||
void Func1(); | ||
void Func4(){}; | ||
void Func2(T a); | ||
void Func3(T a){}; | ||
static void staticFunc1(); | ||
static void staticFunc4(){}; | ||
static void staticFunc2(T a); | ||
static void staticFunc3(T a){}; | ||
}; | ||
template<typename T> | ||
void TypeClass<T>::Func1(){} | ||
|
||
template<typename T> | ||
void TypeClass<T>::Func2(T a){} | ||
|
||
template<typename T> | ||
void TypeClass<T>::staticFunc1(){} | ||
|
||
template<typename T> | ||
void TypeClass<T>::staticFunc2(T a){} | ||
|
||
template class TypeClass<int>; // 实例化声明,会实例化模板类 | ||
|
||
int main(){ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
ref: [C++template教程](https://qixinbo.info/2017/07/09/cplusplus-template/) | ||
|
||
- 类模板中的友元 | ||
1. 非模板函数/类的友元 `main51.cpp` | ||
普通函数/类作为类模板的友元,没啥可讲的 | ||
2. 约束性模板函数/类的友元 `main52.cpp` | ||
模板函数/类作为类模板的友元,只有type相同的类模板和友元之间才能成为朋友,可以理解为在类模板中使用type对友元函数进行实例化 | ||
3. 非约束性模板函数/类的友元 `main.cpp` | ||
模板函数/类作为类模板的友元, 即便时不同的type的类模板和友元之间也能成为朋友 | ||
# 类模板中的友元 | ||
1. 非模板函数/类的友元 `main51.cpp` | ||
普通函数/类作为类模板的友元,没啥可讲的 | ||
2. 约束性模板函数/类的友元 `main52.cpp` | ||
模板函数/类作为类模板的友元,只有type相同的类模板和友元之间才能成为朋友,可以理解为在类模板中使用type对友元函数进行实例化 | ||
3. 非约束性模板函数/类的友元 `main.cpp` | ||
模板函数/类作为类模板的友元, 即便时不同的type的类模板和友元之间也能成为朋友 | ||
|
||
# 模板函数/类 | ||
1. 模板函数/类的定义和声明通常要放在一起。 | ||
2. 可以前向声明模板函数/类的存在:`template <typename T> class MyTemplateClass;`, 但是如果只是声明,是无法实例化这个模板的 | ||
3. 模板函数/类可以提前实例化,从而避免在使用时才进行实例化。`template int h<int>(int a, int b);` | ||
4. 每个翻译单元中使用模板时,都会对模板函数/类实例化,在链接时会处理这些重复的实例化,从而保证此项目中每个类型只有一个实例化 |