[2016-04-23] [Some things about C + + class templates]

Source: Internet
Author: User

[2016-04-23] [Some things about C + + class templates]

Definitions (about class templates and template classes)
    • Class Template: The template type is used in the class, that is, the following structure is the class template
         
           
          
      1. template <typename T>
      2. class Array{
      3. T* p;
      4. } ;
    • Template class: Object after instantiation of class template is called template class
Use
    The
    • instantiation must specify a type,
      • with the class name followed by the < type, to indicate the type
         
               
              
        1. array <int> ; a
    • Note
      • No specified type is not instantiated,
      • A variable that is not instantiated is not available!!!
 
   
  
  1. template <typename T> T a;
  2. cin>>a;//‘ERROR:a还没有实例化‘,
    • At the same time the above t A; this usage can only appear in the class, because the class theorem has not been instantiated, outside the class is not allowed!!!
Overloading of operators
  • Overloading of ordinary operators
    • As with other classes of overloads
  • << and >> heavy-duty
      • If the same overloads as the other classes prompt the following error:
           
               
              
        1. error C2679: 二进制“<<”: 没有找到接受“Array<T>”类型的右操作数的运算符(或没有可接受的转换)
      1. Is the statement template T does not play a role, unrecognized, here because the ability problem can not be figured out, but can find a solution, (tube He why, learn the right posture is OK)

      2. Now that we can't recognize it, we'll just let it be recognized, okay?
      3. The method is to overload the function into a function outside the class (if you want to access private, declare it as friend),
      4. Then define the type of the array when declaring the variable (see array A below, not array a)
    • Here is the function declaration called function template of << overloaded

            
           
      1. friend ostream & operator<<(ostream &os,const Array<T> & a){
      2. os<<"hongyang\n";
      3. return os;
      4. }
    • Another problem is that the declaration and definition of the entire friend function must be written in the class.
    • The declaration is written inside, defining the outside of the write band class, which is wrong!!!
      • The reason is that if implemented outside of the class, the outside T and T in the inside is not the same T, the equivalent of defining a new template T (and the class inside the T is not a thing),
      • A bit around, do not understand to jump over, learn the right posture is good
    • If you simply want to define outside the class, there is no way to do so, that is, in this function does not use the class's data members, by using the class member function to achieve the purpose of accessing the data
            
           
      1. friend ostream & operator<<(ostream &os,const Array<T> & a){
      2. // os<<a.Date;错误!!!
      3. os<<a.getDate();
      4. return os;
      5. }

Information:
Http://www.cnblogs.com/xkfz007/articles/2534322.html



From for notes (Wiz)

[2016-04-23] [Some things about C + + class templates]

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.