当前位置:网站首页>Class as a non type template parameter of the template
Class as a non type template parameter of the template
2022-06-12 07:06:00 【Fat fat is the sun】
Template parameter extension
We mentioned earlier : We cannot use a custom class type as a non type template parameter , But let's think about “ Can you put the static of the class / Non static members are used as non type template parameters ”?
① Take the non static member of the class type as the non type template parameter
#include <iostream>
using namespace std;
class Student
{
public:
int age;
public:
Student() :age(0) {};
void ShowInf();
};
void Student::ShowInf()
{
cout << this->age << endl;
}
template <typename T,typename F>
void Test(T& obj, F Func)
{
(obj.*Func)();
}
int main()
{
Student obj;
Test(obj, &Student::ShowInf);
} Be careful : A normal member function cannot exist without a class object , So we have to pass in a class object in the function , Let this class object call ordinary member functions of the class .
② Take the static member function of the class type as the non type parameter of the template
#include <iostream>
using namespace std;
class Student
{
public:
static int age;
public:
static void ShowInf();
};
int Student::age = 0;
void Student::ShowInf()
{
cout << Student::age << endl;
}
template <typename F>
void Test(F Func)
{
Func();
}
int main()
{
Test(&Student::ShowInf);
} Be careful : Because static member functions do not need to depend on class objects , So we can treat static member functions as ordinary functions .
③ Use the public member data of the class as a non type template parameter
#include <iostream>
using namespace std;
class Student
{
public:
int age;
public:
Student() :age(0) {};
};
template <int Student::*obj>
void ShowInf()
{
cout << obj << endl;
}
int main()
{
ShowInf<&Student::age>(); // Output 1 Not what we want to see 0
} Be careful :

Why the output results are different , because “ Member pointer ” It is not a definite address, but relative to the class this The offset of the pointer , See :
④ Take the static member data of the class as the non type parameter of the template
#include <iostream>
using namespace std;
class Student
{
public:
static int age;
};
int Student::age = 0;
template <int& obj> // Remember to add & quote
void ShowInf()
{
cout << obj << endl;
}
int main()
{
ShowInf<Student::age>();
} Be careful : Remember to add & quote !
We can only quote / Pointer to access static variables , We use a pointer to access a static member variable :
#include <iostream>
using namespace std;
class Student
{
public:
static int age;
};
int Student::age = 0;
template <int* obj> // The parameter is a pointer
void ShowInf()
{
cout << *obj << endl;
}
int main()
{
ShowInf<&Student::age>();
} About static Detailed resolution of static members :
边栏推荐
- 9 Sequence container
- When SQL server2019 is installed, the next step cannot be performed. How to solve this problem?
- descheduler 二次调度让 Kubernetes 负载更均衡
- Host computer development (firmware download software requirement analysis)
- 【数据聚类】本专栏中涉及数据集、可视化及注意事项
- [image denoising] image denoising based on partial differential equation (PDE) with matlab code
- leetcode.39 --- 组合总和
- 5 statement
- VSCode常用插件
- Throw away the ugly toast. The movable toast is more interesting
猜你喜欢

Upload file (post form submission form data)

Vscode Common plug - in

Interview intelligence questions
![Leetcode: Sword finger offer 66 Build product array [application of pre and post infix]](/img/de/cd98d4d86017a13ec4172ba3054e99.png)
Leetcode: Sword finger offer 66 Build product array [application of pre and post infix]

Scons编译IMGUI

When SQL server2019 is installed, the next step cannot be performed. How to solve this problem?

leetcode:剑指 Offer 66. 构建乘积数组【前后缀积的应用】

Kali与编程:如何快速搭建OWASP网站安全实验靶场?

Troubleshooting of cl210openstack operation -- Chapter experiment

1. Foundation of MySQL database (1- installation and basic operation)
随机推荐
Decoupling in D
初中学历,从不到3K,到月薪30K+,不设限的人生有多精彩
C language pointer
node:打不开/node:已拒绝访问
NOI openjudge 计算2的N次方
RT thread studio learning (VII) using multiple serial ports
[image denoising] salt and pepper noise image denoising based on Gaussian filter, mean filter, median filter and bilateral filter with matlab code attached
(14)Blender源码分析之闪屏窗口显示软件版本号
2021 robocom world robot developer competition - undergraduate group (Preliminary)
应届生苦恼:是去华为拿1万多低薪,还是去互联网拿2万多高薪
【图像检测】基于深度差分和PCANet实现SAR图像变化检测附matlab代码
Idea common shortcut keys
Stm32cubemx learning (I) USB HID bidirectional communication
esp32 hosted
Curry carries the fourth game of the warriors against the Celtics
Scons编译IMGUI
VSCode常用插件
RT thread studio learning (I) new project
[image detection] SAR image change detection based on depth difference and pcanet with matlab code
Unable to load bean of class marked with @configuration