The use of C + + template class in the study of non-fine and shame

Source: Internet
Author: User

My intermittent use of C + + also has a period of time, some time to create a sense of complacency, think of the language features of C + + has been known almost, in the grammatical aspects of the area has nothing to beat me, now what to do is based on practical problems to write procedures, the problem lies in the design and analysis of the algorithm, Is the strategy to solve the problem. However, this afternoon's experience for oneself blow: never complacent, to maintain a humble learning heart.

1 The problem arises

When I implemented the contiguous structure of the 219-page list in [book][1], I wrote two files according to my inertia: rblist.h and Rblist.cpp, respectively, for the class definition and the concrete implementation of the method. The definition of the template class is included in the rblist.h. I was having trouble writing the test program testList.cpp to test the table structure of this contiguous storage.

The compilation commands I use are:

g++ Utility.cpp rblist.cpp testList.cpp -o testList

And in TestList.cpp I used #include "rblist.h" this preprocessing directive, the compilation error is as follows:

I confirm the code several times, check the number of times or the compilation does not pass. I also cut the code in Rblist to one remaining method and still compile. There is no way to search for solutions through the Internet. If the solution is found, the code and the compile command remain the same, and only the included instruction in the testList.cpp is changed to #include "rblist.cpp" a successful compilation.

I feel very strange about the practice of including CPP files in pre-contained directives. I've never seen a command that often contains a header file that can be used to include a CPP file, and is guaranteed to compile successfully, and the friend on the web points out that this is an easy mistake to make using template classes. I have doubts about this and I have never seen such a usage in any textbooks I have read, and #include "rblist.cpp" I feel very uncomfortable with this usage. I decided to explore the matter.

2 on the use of template classes in the understanding

I've read [Bruce Eckel's book][2], and the introduction to the template class has been blurred, but with my gut instinct, I'm not going #include "rblist.cpp" to use it in this way, and I'm actually validating my intuition. The book gives 4 ways to use the template class:

  1. Inline function Definition: Writes the definition of a method to the declaration of a class and writes the main () function to the template class in the same file. This approach is easy to use and usually appears in textbooks, where the template class is not used in actual engineering projects at all. When I started learning C + +, I practiced the use of template classes in this way, and it was precisely because of this method that I was mistaken that I had mastered the use of template classes.
  2. Non-inline function Definitnion: This method defines a class function outside of the class declaration, the function definition and the class declaration are still in the same file, and the main () function used for testing is also in the same file, and the last whole file is the CPP file. This method gives a new perspective on the definition of template classes, separating functions from class declarations, but because they are still in the same file, there is still no command to appear in the header of the source file #include 模板类 . This method and the previous method do not produce the problems that are encountered in this article.
  3. Header files: This method writes the entire template class declaration and definition uniformly in a header file. h. The original says, "Don ' t put in anything that allocates storage," but template definitions is special. anything preceded by template<.. > means the compiler won ' t allocate storage for it in that point, but would instead wait until it ' s told to. So you'll almost always put the entire template declaration and definition in the header file, for ease of use. I used this method to re-write a file RbList2.h file, the template list of the declaration and definition of all in the inside, and then use #include "rblist2.h" to include it in the TestList.cpp, using the g++ Utility.cpp testList.cpp -o testList compilation, smoothly through the compilation. I am more recognized in this way using template classes. I see the previous reading [book][2] The corresponding sentence above the mark, but one to the actual problem, or forget it. It seems that, to learn the language, light to see not practice is not good, to see the basic knowledge of the premise of a lot of programming, through practical programming experience to improve the ability of programming. For example, I encountered the problem of this article in the programming, back to see the previous read books, after such a cycle of repeated process, the corresponding questions to understand the profound. It was easy to think of the concepts and knowledge points that were difficult and very tall when I was a beginner. Knowing the background of the concept, its benefits to programmers, and so on, is more profound than just knowing how to use it, and will not forget it.
  4. [Bruce Eckel] [2] It is also mentioned that the template class declaration and definition are written in the. h file and the. cpp file, but this is only for the purpose of generating DLLs and have to do the compromise approach, that the method needs to refer to specific compiler provisions. This method is seldom seen, I think this is probably #include "rblist.cpp" the use of it.
3 sentiment

Through this experience today, I realized that:

    1. It is important to keep a humble heart forever. Don't be complacent, stop learning where you are studying.
    2. The best way to learn programming is to do a lot of programming, the more the compilation, where the sense of mystery will become more and more less, they will become more and more full.

[1]: Robert L. Kruse and Alexander J. Ryba. Data Structures and program design in C + +. Upper Saddle River, 2000.
[2]: Bruce Eckel. Thinking in C + + Volume 1. 2nd. 2000.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The use of C + + template class in the study of non-fine and shame

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.