The use of C + + template class in the study of not fine and ashamed

Source: Internet
Author: User
Tags class definition function definition

I have been using C + + intermittently for a while. There are times when there is a feeling of complacency. Think of the C + + language features already know almost the same, there is nothing in the grammar of my place, now what to do is based on the actual problem of the code, the problem lies in the design and analysis of the algorithm. is a strategy to solve this problem.

However, this afternoon's experience for oneself blow: never complacent, to maintain a humble learning heart.

1 The problem arises

When I implement the contiguous structure of the list on page 219 of [book][1]. According to my inertia, wrote two files: Rblist.h and Rblist.cpp, respectively, for the class definition and the detailed implementation of the method. The definition of the template class is included in the rblist.h. I also wrote a test program testList.cpp to test the continuous storage of the table structure encountered trouble.

The compilation commands I use are:

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

And in TestList.cpp, I'm using #include "rblist.h" this preprocessing directive. Compile errors such as the following:

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 only one method still compiled just. There is no way to search the solution only by the Internet. If you find a way to solve it. The code and the compile command remain unchanged. Instead, only the include command in TestList.cpp is changed to #include "rblist.cpp" a successful compilation.

I am very surprised by the practice of including CPP files in pre-include directives.

I've never seen a command that often includes a header file can be used to include a CPP file and can guarantee a successful compilation. And the friend on the net pointed out that this is the mistake of using template class easy to make. I have a question about this. I have never seen such a usage in any textbook I have ever read #include "rblist.cpp" , and I feel very uncomfortable with this usage.

I decided to explore the problem.

2 on the use of template classes in understanding

I have read [Bruce Eckel's book][2], and the introduction of the template class is blurred. But with my intuition. I'm certainly not using #include "rblist.cpp" the template class 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 at the same time writes the main () function to the template class in the same file. This approach is easy to use, often in textbooks where the template classes are not used in actual project projects in this way. When I was a beginner of C + +, I practiced the use of template classes by this method. It is because of this method that I mistakenly feel that I have mastered the use of template classes.

  2. non-inline Function Definitnion: This method defines a class function outside the class declaration, and the function definition and class declaration are still in the same file. The main () function used for testing at the same time is also in the same file. Finally the entire file is a CPP file. This approach gives a new perspective on the definition of template classes. Detach a function from the class declaration. However, because they are still in the same file, the #include template class command is still not required at the head of the source file.

    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 use this method to write a file RbList2.h file again, the template list of the declaration and definition are all written in the inside. Then use #include "rblist2.h" to include it in TestList.cpp. Compiled with g++ Utility.cpp testlist.cpp-o testlist , and successfully compiled. I am more than recognized in this way using template classes. I see the mark that I used to read the corresponding sentence in [book][2], but when it comes to the actual problem, it's forgotten.

    appears. If you want to learn the language, it is not possible to see the light, to see the basic knowledge under the premise of a lot of programming, through practical programming experience to improve the programming ability. For example, I encountered the problem of this article in programming, looking back to see the book, after such a cycle of repetition of the process, the corresponding questions to understand the profound.

    The concept and knowledge points that were very difficult and very tall at the beginning of a beginner's day became very easy.

    Knows the background of the concept, its advantages to programmers, and so on, more profound than just knowing how to understand it in detail, 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 to produce a DLL and have to do the compromise approach, that the method needs to refer to the detailed compiler provisions. Such a method is very rare. I think that might be #include "rblist.cpp" the way to use 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 studying at the place of study.

    2. The best way to learn programming is to do a lot of programming. The more you weave, the less you will feel, and the more you will become.

[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.

The use of C + + template class in the study of not fine and ashamed

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.