当前位置:网站首页>Class template parsing
Class template parsing
2022-07-22 20:23:00 【Menon poet】
Call time of member function in class template
A class template is an abstraction of a group of classes with only different member data types , Basic conceptual things will not be repeated in this chapter . This chapter mainly explores the member function call timing in the class template , Let's take a look at the code case .
#include <iostream> using namespace std; class Person1 { public: void showPerson1(){ cout << "show person1" << endl;}; }; class Person2 { public: void showPerson2(){ cout << "show person2" << endl;}; }; // Define your own class template template <class T> class Myclass { public: // Member functions in class templates void func1(){ obj_p.showPerson1();}; void func2(){ obj_p.showPerson2();}; private: T obj_p; }; int main() { return 0; }
In the above code main Function directly returns ; Let's compile it, as shown in the figure below :
The above code compilation will not report an error . But the member variables in the member function of the class template in the above code obj_p Called Person1 class , Also called Person2 class , This indicates that the member function of the class template is not called to the member function during compilation .
Next, we will further test , take main The function is changed to :
int main() { Myclass<Person1> p1; p1.func1(); p1.func2(); return 0; }
The function compilation result is as follows :
The reason for the error of the above results is Person1 Not in class showPerson2 Function of .
The above two results further prove : Compiler at compile time , The type of member variables in the template class is uncertain , Therefore, the member functions in the class template will not be detected , When a class template instantiates an object , At this time, the type of the member variable of the object has been determined , When calling the corresponding member function, the compiler will determine whether it is legal .
We will go further main The function is changed as follows :
int main() { Myclass<Person1> p1; p1.func1(); Myclass<Person2> p2; p2.func2(); return 0; }
The function compilation result is as follows :
Summary of the above test results : Member functions in class templates are not created from the beginning , Create it when called .
Class templates are written in files
As we know from the above chapters , Functions in class templates are created only when called , This will lead to the link of sub file compilation not . Next, let's take a case .
as follows main.cpp Code :
#include <iostream> #include <string> using namespace std; template<class T1, class T2> class Person { public: Person(T1 name, T2 age); void showPerson(); private: T1 m_name; T2 m_age; }; template<class T1, class T2> Person<T1, T2>::Person(T1 name, T2 age) { this->m_name = name; this->m_age = age; } template<class T1, class T2> void Person<T1, T2>::showPerson() { cout << "name:" << this->m_name << ", age:" << this->m_age << endl; } int main() { Person<string, int> p1("lijd", 18); p1.showPerson(); return 0; }
The above program is compiled 、 No problem running , give the result as follows :
After the above code is written in files , The code structure is as follows :
// The file named person.h #ifndef _PERSON_H_INCLUDED #define _PERSON_H_INCLUDED #include <iostream> #include <string> using namespace std; template<class T1, class T2> class Person { public: Person(T1 name, T2 age); void showPerson(); private: T1 m_name; T2 m_age; }; #endif // _PERSON_H_INCLUDED
// The file named person.cpp #include "person.h" template<class T1, class T2> Person<T1, T2>::Person(T1 name, T2 age) { this->m_name = name; this->m_age = age; } template<class T1, class T2> void Person<T1, T2>::showPerson() { cout << "name:" << this->m_name << ", age:" << this->m_age << endl; }
// The file named main.cpp #include "person.h" int main() { // Person<string, int> p1("lijd", 18); // p1.showPerson(); return 0; }
The result of trying to compile is as follows :
The results of the analysis : This is because of the above Member functions in class templates are not created from the beginning , Create it when called . because main.cpp The file contains only person.h, Not included person.cpp, When the compiler calls, the function implementation is not found and an error is reported .
There are two ways to solve this mistake :1、 Reference directly in the file person.cpp, This method can solve , But it's not usually written like this .2、 take person.h Statement in with person.cpp All the implementations in are written in person.hpp in ,main As long as the function contains person.hpp That's all right. , The general default is hpp File is a class template file .
The modified code is as follows :
// The file named person.hpp #ifndef _PERSON_HPP_INCLUDED #define _PERSON_HPP_INCLUDED #include <iostream> #include <string> using namespace std; template<class T1, class T2> class Person { public: Person(T1 name, T2 age); void showPerson(); private: T1 m_name; T2 m_age; }; template<class T1, class T2> Person<T1, T2>::Person(T1 name, T2 age) { this->m_name = name; this->m_age = age; } template<class T1, class T2> void Person<T1, T2>::showPerson() { cout << "name:" << this->m_name << ", age:" << this->m_age << endl; } #endif // _PERSON_HPP_INCLUDED
// The file named main.cpp #include "person.hpp" int main() { Person<string, int> p1("lijd", 18); p1.showPerson(); return 0; }
The program execution and running results are as follows :
边栏推荐
- RetinaNet:Focal Loss for Dense Object Detection
- Getting started with CSDN markdown editor
- linux安装oracle XE
- How to deal with DNS hijacking, DNS hijacking, and DNS hijacking solutions
- ping: www.baidu. Com: unknown name or service reason analysis
- 数据开发常用工具---查询结果邮件定时发送
- 什么是百度快照劫持?百度快照劫持原理和解决办法
- YOLO v1、v2、v3
- Don't hack the website. How to solve it? How to deal with the problem of website being hacked
- DNS劫持如何预防、DNS是什么?DNS劫持详解
猜你喜欢
YOLO v1、v2、v3
图的深度优先搜索和广度优先搜索
How to deal with DNS hijacked? Five ways to deal with it
求N!后面有多少个0
What is network hijacking? How to repair web pages that have been tampered with and hijacked (final scheme) how to repair web page hijacking?
机器学习入门:逻辑回归-2
Chinese search for introduction to elastic search (8)
她力量系列三丨把握当下,坚持热爱,与食物图像识别结缘的科研之路
How to deal with DNS hijacking, DNS hijacking, and DNS hijacking solutions
dns被劫持有什么现象?DNS是什么 dns被劫持了如何解决
随机推荐
图像质量评估
Nc26 bracket generation
Replace some escape characters with characters of the specified standard
Elastic Search 学习入门之ES的简单操作命令(二)
AMBert
canny边缘检测
《预训练周刊》第38期: Transformer、BERT结构优化
Introduction to machine learning: Logistic regression-2
LeetCode21——Merge two sorted lists——Iteration or recursion
类模板剖析
数据开发常用工具---查询结果邮件定时发送
dns劫持如何完美修复?dns被劫持如何解决如何完美修复
CPU亲和力
LeetCode160 & LeetCode141——double pointers to solve the linked list
Chinese search for introduction to elastic search (8)
LeetCode53——Maximum Subarray——3 different methods
Microblaze添加自定义IP核,挂AXI总线实现SSD1306 OELD驱动
Core concepts of elastic search learning Introduction (4)
The setting of node.master and node.data in the production environment of the introduction to elastic search (3)
dns 劫持什么意思、dns 劫持原理及几种解决方法