Implementation of C + + template class header file and implementation of file separation method

Source: Internet
Author: User
Tags vc9

How to implement C + + template class header file and implement file separation, this problem and compiler related.

Citing <<c++primer (fourth edition) >> Views: 1) Standard C + + defines two models for compiled template code: the "include" model and the "compile separately" model. 2) All compilers support the "include" model, and some compilers support the "compile separately" model.

Issue: (Post in: http://topic.csdn.net/u/20101215/15/f4f270f2-f0f9-4c5f-8765-1bfde2aeebbf.html)

The first method: Press the "include" model in C++primer to define the last line phrase in the header file of the template class: #include "Template_compile.cpp"

In the class template header file Template_compile.h:

[CPP]View Plaincopyprint?
    1. template<class t>
    2. Class Base
    3. {
    4. Public
    5. Base () {};
    6. ~base () {};
    7. T Add_base (t x,t y);
    8. };
    9. #include "Template_compile.cpp"

In the implementation file Template_compile.cpp of the class template:

[CPP]View Plaincopyprint?
    1. template<class t>
    2. T Base<t>::add_base (t x,t y)
    3. {
    4. return x+y;
    5. }

In the test file use_template.cpp that uses the template:

[CPP]View Plaincopyprint?
    1. #include <iostream>
    2. #include "template_compile.h"
    3. Using namespace std;
    4. void Main ()
    5. {
    6. base<int> bobj;
    7. Cout<<bobj.add_base (2,3) <<endl;
    8. }

This method cannot be compiled, "template_compile.cpp" file cannot "see" "template_compile.h" file.

However: If I put the code in the implementation file of the class template in the header file of the class template, comment out: #include "template_compile.cpp", compile and run without any errors. In theory, "put the code in the implementation file of the class template in the header file of the class template" and "the last line phrase in the header file that defines the template class: #include" Template_compile.cpp "" is consistent, but the compiler does not pass.

The experiment proves that VC9.0 does not support the "inclusion" model as described in C++primer.

The second method: Bruceteen proposed: Use define

In the class template header file Template_compile.h:

[CPP]View Plaincopyprint?
    1. template<class t>
    2. Class Base
    3. {
    4. Public
    5. Base () {};
    6. ~base () {};
    7. T Add_base (t x,t y);
    8. };
    9. #define Fuck
    10. #include "Template_compile.cpp"

In the implementation file Template_compile.cpp of the class template:

[C-sharp]View Plaincopyprint?
    1. #ifdef Fuck
    2. template<class t>
    3. T base<t>::add_base (t x,t y)
    4. {
    5. return x+y;
    6. }
    7. #endif

The test file does not change.

The experiment proves that this method can separate the class template header file and the implementation file in VC9.0.

Method Three:

In the class template header file Template_compile.h:

[CPP]View Plaincopyprint?
    1. template<class t>
    2. Class Base
    3. {
    4. Public
    5. Base () {};
    6. ~base () {};
    7. T Add_base (t x,t y);
    8. };

In the implementation file Template_compile.cpp of the class template:

[C-sharp]View Plaincopyprint?
    1. #include "template_compile.h"
    2. template<class t>
    3. T base<t>::add_base (t x,t y)
    4. {
    5. return x+y;
    6. }

In the test file use_template.cpp using the template: Use # include "Template_compile.cpp"

[C-sharp]View Plaincopyprint?
    1. #include <iostream>
    2. #include "Template_compile.cpp"
    3. Using namespace std;
    4. void Main ()
    5. {
    6. base<int> bobj;
    7. Cout<<bobj.add_base (2,3) <<endl;
    8. }

Experimental results show that this method can realize the separation of the class template header file and the implementation file in VC9.0.

In addition, the experiment proves that VC9.0 does not support the "compile separately" model.

Implementation of C + + template class header file and implementation of file separation method

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.