类的成员作模板的非类型模板参数

模板参数扩展

我们前面提到:我们不可以将自定义类类型作为非类型模板参数,但是我们想想“可不可以把类的静态/非静态成员部分作为非类型模板参数呢”?

① 将类类型的非静态成员作为非类型模板参数

#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);  
}  

注意:普通成员函数不能离开类对象而存在,因此我们必须在函数中传入一个类对象,让这个类对象去调用类的普通成员函数。

② 将类类型的静态成员函数作为模板的非类型参数

#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);  
}  

注意:由于静态成员函数不需要依赖于类对象,因此我们可以把静态成员函数当作普通函数来看待。

③ 将类的公有成员数据作为非类型模板参数

#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>();  // 输出1而非我们想看到的0
}  

注意:

为何输出结果不同,因为“成员指针”并不是一个确定的地址而是相对于类的this指针的偏移量,详见:

什么是“成员指针”?

④ 将类的静态成员数据作为模板的非类型参数

#include <iostream>  
using namespace std;  
  
class Student  
{  
public:  
    static int age;  
};  
  
int Student::age = 0;  
  
template <int& obj>  // 切记要加&引用
void ShowInf()  
{  
    cout << obj << endl;  
}  
  
int main()  
{  
    ShowInf<Student::age>();  
}  

注意:切记要加&引用!

我们只能通过引用/指针来访问静态变量,我们使用指针在访问一次静态成员变量:

#include <iostream>  
using namespace std;  
  
class Student  
{  
public:  
    static int age;  
};  
  
int Student::age = 0;  
  
template <int* obj> // 参数为指针  
void ShowInf()  
{  
    cout << *obj << endl;  
}  
  
int main()  
{  
    ShowInf<&Student::age>();  
}  

关于static静态成员的详细解析:

static静态成员详解

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肥肥胖胖是太阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值