Non-Template Friend Classes and Functions in C++

It is possible to declare any class or function to be a friend to your template class. Each instance of the class will treat the friend pro...

It is possible to declare any class or function to be a friend to your template class. Each instance of the class will treat the friend properly, as if the declaration of friendship had been made in that particular instance. Listing 19.3 adds a trivial friend function, Intrude(), to the template definition of the Array class, and the driver program invokes Intrude(). Because it is a friend, Intrude() can then access the private data of the Array. Because this is not a template function, it can only be called on Arrays of int.

NOTE: To use Listing 19.3, copy lines 1-26 of Listing 19.2 after line 1 of this listing, and then copy lines 51-86 of Listing 19.2 after line 37 of this listing.

Listing 19.3. Non-template friend function.

1: // Listing 19.3 - Type specific friend functions in templates
2:
3: template <class T> // declare the template and the parameter
4: class Array // the class being parameterized
5: {
6: public:
7: // constructors
8: Array(int itsSize = DefaultSize);
9: Array(const Array &rhs);
10: ~Array() { delete [] pType; }
11:
12: // operators
13: Array& operator=(const Array&);
14: T& operator[](int offSet) { return pType[offSet]; }
15: const T& operator[](int offSet) const
16: { return pType[offSet]; }
17: // accessors
18: int GetSize() const { return itsSize; }
19:
20: // friend function
21: friend void Intrude(Array<int>);
22:
23: private:
24: T *pType;
25: int itsSize;
26: };
27:
28: // friend function. Not a template, can only be used
29: // with int arrays! Intrudes into private data.
30: void Intrude(Array<int> theArray)
31: {
32: cout << "\n*** Intrude ***\n";
33: for (int i = 0; i < theArray.itsSize; i++)
34: cout << "i: " << theArray.pType[i] << endl;
35: cout << "\n";
36: }
37:
38: // driver program
39: int main()
40: {
41: Array<int> theArray; // an array of integers
42: Array<Animal> theZoo; // an array of Animals
43: Animal *pAnimal;
44:
45: // fill the arrays
46: for (int i = 0; i < theArray.GetSize(); i++)
47: {
48: theArray[i] = i*2;
49: pAnimal = new Animal(i*3);
50: theZoo[i] = *pAnimal;
51: }
52:
53: int j, k;
54: for (j = 0; j < theArray.GetSize(); j++)
55: {
56: cout << "theZoo[" << j << "]:\t";
57: theZoo[j].Display();
58: cout << endl;
59: }
60: cout << "Now use the friend function to ";
61: cout << "find the members of Array<int>";
62: Intrude(theArray);
63:
63: // return the allocated memory before the arrays are destroyed.
64: for (k = 0; k < theArray.GetSize(); k++)
65: delete &theZoo[j];
66:
67: cout << "\n\nDone.\n";
68: return 0;
69: }


Output: theZoo[0]: 0
theZoo[1]: 3
theZoo[2]: 6
theZoo[3]: 9
theZoo[4]: 12
theZoo[5]: 15
theZoo[6]: 18
theZoo[7]: 21
theZoo[8]: 24
theZoo[9]: 27
Now use the friend function to find the members of Array<int>
*** Intrude ***
i: 0
i: 2
i: 4
i: 6
i: 8
i: 10
i: 12
i: 14
i: 16
i: 18

Done.

Analysis: The declaration of the Array template has been extended to include the friend function Intrude(). This declares that every instance of an array will consider Intrude() to be a friend function; thus, Intrude() will have access to the private member data and functions of the array instance.

On line 33, Intrude() accesses itsSize directly, and on line 34 it accesses pType directly. This trivial use of these members was unnecessary because the Array class provides public accessors for this data, but it serves to demonstrate how friend functions can be declared with templates.
Name

ADO,131,ASP,3,C++,61,CORE JAVA,1,CSS,115,HTML,297,index,5,JAVASCRIPT,210,OS,47,PHP,65,SAD,53,SERVLETS,23,SOFTWARE ENGINEERING,245,SQL,71,TCP/IP,1,XHTML,9,XML,18,
ltr
item
Best Online Tutorials | Source codes | Programming Languages: Non-Template Friend Classes and Functions in C++
Non-Template Friend Classes and Functions in C++
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/05/non-template-friend-classes-and.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/05/non-template-friend-classes-and.html
true
357226456970214079
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content