Class template

xiaoxiao2021-03-30  197

Here, the definition and implementation of the following templates are discussed here.

In the VC, the compiler does not require the definition and implementation of the template class to be implemented in one .h header file or .cpp file (unlike some information), the VC compiler requires you to call templates. Module Include All implementations files on templates, only this requirement. Standard C supports separate compilation, but template implementation to add export, this keyword is not supported in VC.

Mid-type template implementation and defined examples below / **** File Template.h ******* / Template class mytemplate {/ ******** /};

/ **** file template.cpp ******* / # include "template.h" template mytemplate :: myfun () {/ *** /} / **** Other Codes *** /

/ ****** file user.cpp ******* / # include template.cpp // pay attention, here is .CPP implementation file / **** Other Codes ***** /

The CT template implementation and definition is not used in one .h or .CPP, this is also the most common method of usual.

Standard C mid-class template implementation and defined example below / **** file template.h ******* / template class mytemplate {/ ******** /};

/ **** File Template.cpp ******* / # include "Template.h" // Note, here is used with export keywords, VC does not support. Export Template MyTemplate :: myfun () {/ *** /} / **** Other Codes *** /

/ ****** file user.cpp ******* / # include template.h // pay attention, here is .h declaration definition file / **** Other Codes ***** /

As for the standard C class template implementation and definitions in one .h or .CPP, the examples are completed, and VCs are the same. The class template is compiled and implemented in one file in the user function, but the compiler has a large distinction in both methods. Separate compilation, can save compilation time, only single compilation (in fact, the separation implementation method of the VC above only feels, the compiler is in the same way is exactly the same, so the VC compiler is in pair Class templates will never be able to build a single size) reduces the size of the generated file, but the overhead will be slightly larger. In one .h or .CPP fully realizes a class template in some time that will make the generated executable to become too large, but save a little overhead. Therefore, these two methods have their own advantages and disadvantages. When appropriate, you can make a choice according to the situation. This should be provided: whether it is compiled as a unit or is divided into some independent compilation units, these code should be able to work in the same way. Close to this ideal way should limit the dependence of the template definition to its environment, avoiding too much environment information in the process of entering instantiation, Bjarne said. In my opinion, this is also an independence of data from the perspective of class template.

For the first time, welcome to discuss!

转载请注明原文地址:https://www.9cbs.com/read-130541.html

New Post(0)