成员函数模板,显示实例化与声明
发布日期:2021-05-10 02:08:27 浏览次数:15 分类:精选文章

本文共 2540 字,大约阅读时间需要 8 分钟。

C++ Template������������

���������������������������������

���C++������������������������������������������������������������������������������������������

#include "pch.h"
#include
using namespace std;
class B {
public:
int a;
template
void fun(U obj) {
cout << "void fun(U obj)" << endl;
cout << obj << endl;
}
template
void func(U obj);
};
template
void B::func(U obj) {
cout << "void B::func(U obj)" << endl;
cout << obj << endl;
}
int main() {
B b;
cout << sizeof(b) << endl;
b.fun(1);
b.func(2);
return 0;
}

������������

  • ������������������������������������template���������
  • ������������������������������������

���������������������������������

������������������������������������������������������������������������

#include "pch.h"
#include
using namespace std;
template
class B {
public:
int a;
template
void fun(U obj) {
cout << "void fun(U obj)" << endl;
cout << obj << endl;
}
template
void func(U obj);
};
template
template
void B::func(U obj) { cout << "void B::func(U obj)" << endl; cout << obj << endl; } int main() { B
b; cout << sizeof(b) << endl; b.fun(1); // ������������������������ b.func(2.0f); // ������������������������ return 0; }

������������

  • ������������������������������������������������������������astle������������
  • ���������������������������������������������������������������������

������������������������������������

������������������������������������������������������������������������������������

#include "pch.h"
#include "test.h"
#include
using namespace std;
template B*
;
// ���������������������������������������
int main() {
B
b;
// b.func(1); // ������������������������������
return 0;
}
#ifndef _TEST_H
#define _TEST_H
#include "pch.h"
#include
using namespace std;
template
class B {
public:
int a;
void fun(T obj) {
cout << "void fun(T obj)" << endl;
cout << obj << endl;
}
};
#endif // !_TEST_H
// test.h���������
#include "pch.h"
#include
using namespace std;
template
void func() {
B
b;
b.fun(1);
}

������������

  • extern template B* ; ������������������������������������
  • ������template B<int>; ���������������������
  • ���������������������������������������������������
上一篇:为什么类的声明与定义写在h文件而不会造成重复定义
下一篇:类模板成员函数定义问题

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2025年04月20日 16时59分14秒