All six forms of class template (class template) friend (friend) in C + +

Source: Internet
Author: User
Tags include

Class template (class template) friend and common class friends, there are many differences,

The main reason is that the class template contains template parameters (template parameter), which causes the friends and class templates to have many matching forms;

It consists of six main forms:

1. Class-Friend (Friend): "Template parameter is the current class" class template;

2. Class-Friend: "Template parameters arbitrary" template class;

3. Template class-friend: "Template parameters are the same" template class;

4. Template class-friend: "Template parameter arbitrary" template class

5. Template class-Friends: Class;

6. Template class-friend: Current template parameters (class); (C++11)

Use the code and the comment specifically,

The code is as follows:

* * * cppprimer.cpp * * Created on:2013.11.24 * author:caroline//*eclipse CDT, gcc 4.8.1*/ #include <iostream> #include <string> template <typename t> class Pal; Pal Class C {friend class pal<c>;//"Class C instantiation" Pal, template for C <typename t> Frien D class Pal2;  
All instantiations of the Pal2 class are friends of C private:void print () {std::cout << "class C" << Std::endl;}  
      
}; Template <typename t> class C2 {friend class pal<t>;//"Same instantiation as the C2 class", for all instantiations of C2 's friend//pal2 class,  
    Are C2 friends, note that template parameters (X) can not be the same template <typename x> friend class Pal2; Friend class Pal3; Common friend T;  
c++11 template type parameter friend Private:void print () {std::cout << "class C2" << Std::endl;}  
      
};  
    Template <typename t> class Pal {C MyC; C2<t> myC2; Must be T//c2<double> myC2; Cannot use public:void PRINTC when instantiated differently() {Std::cout << "This is class Pal:";  
    Myc.print ();  
        } void PrintC2 () {std::cout << "This is class Pal:";  
    Myc2.print ();  
      
}  
};  
    Template <typename t> class Pal2 {C MyC;  
C2<double> myC2;  
        Public:void Printc () {std::cout << "This is class Pal2:";  
    Myc.print ();  
        } void PrintC2 () {std::cout << "This is class Pal2:";  
    Myc2.print ();  
      
}  
};  
Class Pal3 {c2<double> myC2;  
        Public:void printC2 () {std::cout << "This is class Pal3:";  
    Myc2.print ();  
      
}  
}; Class Pal4 {c2<pal4> myC2;//Note Pal4 is a template parameter for the C2 class Public:void printC2 () {std::cout << "t  
        He is class Pal4: ";  
    Myc2.print ();  
      
}  
};  
      
   int main (void) {std::cout << "Hello mystra!" << Std::endl; pal<c> pc; The PAL class must be instantiated as C PC.PRINTC ();  
    You can use//pal<int> PCI; Pci.print (); Error, access to the private member pal2<int> PI2;  
      
    Pal2 class can be arbitrarily instantiated pi2.printc (); pal<int> Pi;  
      
    Has the same example, in the class declaration c2<t> pi.printc2 (); PI2.PRINTC2 ();  
      
    Note that the Pal2 class is instantiated as &LT;INT&GT, and the internal C2 class is instantiated as <double>;  
    PAL3 P3;  
      
    P3.PRINTC2 ();  
    PAL4 P4;  
      
    P4.PRINTC2 ();  
return 0; }

Output:

Hello mystra!  
This is class Pal:class the C this is class Pal2:class the C this is class Pal:class C2 the This is class  
Pal2:cla SS C2 This are  
class Pal3:class C2 This is  
class Pal4:class C2

Author: csdn Blog spike_king

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

Related Article

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.