All six forms and codes of class template friend

http://blog.csdn.net/caroline_wendy/article/details/16916441

All six forms and codes of class template friend

There are many differences between friend of class template and friend of common class.

The main reason is that class templates contain template parameters, which will lead to many matching forms between friends and class templates.

It mainly includes six forms:

1. Class-friend: "Template parameter is the class template of current class";

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

3. Template class-Friend: Template class with the same template parameters;

4. Template Class-Friend: Template Class with Arbitrary Template Parameters

5. Template class-friend: class;

6. Template Class-Friends: Current Template Parameters (Classes); (C++11)

See the code and comments for details.

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 friends
 
class C {
	friend class Pal<C>; //The Al class "instantiated by class C" is the friend of C.
	template <typename T> friend 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>; //The Al class of "the same instantiation as Class 22" is a friend of C2.
	//All instantiations of the Pal2 class are friends of C2. Note that template parameters (X) are not the same.
	template <typename X> friend class Pal2;
	friend class Pal3; //Ordinary friends
	friend T; //C++11 Template Type Parameter Friends
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; // Different instantiation, not usable
public:
	void printC() {
		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 that Pal4 is a template parameter for class C2
public:
	void printC2() {
		std::cout << "this is class Pal4 : ";
		myC2.print();
	}
};
 
int main (void) {
 
	std::cout << "Hello Mystra!" << std::endl;
 
	Pal<C> pc; //The Pal class must be instantiated into C
	pc.printC(); //have access to
	//Pal<int> pci;
	//pci.print(); // error reporting, access to private members
 
	Pal2<int> pi2; //Pal2 class can be instantiated at will
	pi2.printC();
 
	Pal<int> pi; //With the same instantiation, declare C2 < T > in the class
	pi.printC2();
 
	pi2.printC2(); //Note that the Pal2 class is instantiated as <int> and the internal C2 class is instantiated as <double>.
 
	Pal3 p3;
	p3.printC2();
 
	Pal4 p4;
	p4.printC2();
 
	return 0;
}

 

Keywords: P4 Eclipse

Added by Ferdog on Thu, 03 Oct 2019 19:14:23 +0300