In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to deeply analyze the application code of C++ template. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.
There is the concept of classful inheritance in C++, which means an application that can achieve the same function as inheritance.
Sample C++ class template code:
# include "stdafx.h"
# include "Stack.h"
# include
< iostream># include
< string># include
< cstdlib># include
< stdexcept>Int _ tmain (int argc, _ TCHAR* argv [])
{
Try
{
Stack
< int>IntStack
Stack
< std::string>StringStack
IntStack.push (7)
Std::cout
< < "intStack.top() = >"
< < intStack.top() < < std::endl; stringStack.push("Hello!"); std::cout< < "stringStack.top() = >"
< < stringStack.top() < < std::endl; stringStack.pop(); stringStack.pop(); } catch(std::exception const& ex) { std::cerr< < "Exception: "< < ex.what()< < std::endl; return EXIT_FAILURE; } return 0; } C++字符串分割函数具体使用方法总结 C++模板限制实际应用方式探讨 C++反射机制具体实现方法详解 C++获取当前路径实现技巧分享 C++数组参数应用方式探讨 在这段C++类模板的代码中,语句[Stack< int>IntStack;] shows the process of instantiating a class template and defining a variable. Like the instantiation of a function template, the instantiation of a class template must provide the type of required parameters. The instantiated variable can call the member function defined by the class template in the same way as the ordinary variable. It's just important to note that member functions of class templates are instantiated only when called. For the above intStack, since only the push () member function is called, it only instantiates the push member function (except for constructor and parse functions, which are called by default).
This has two advantages:
First, it can save space and time.
Second, for the parameter type of each class template, the operation required by the template is required. For example, if you use the custom class MyClass as a class template Caculator
< T>Parameters of the. Because the Caculator class template requires that the parameter types provided support the "+" and "-" operations. However, your MyClass class only needs to use the "+" operation and does not provide a "-" operation. Thanks to the above rules, your MyClass type can still be used as a Caulator
< T>Parameters of the. The premise is that you do not use the "-" related member function.
The above is how to deeply analyze the application code of C++ template. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.