


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of templates and friends in object-oriented programming (oop) of c++. It covers the rules for declaring functions and classes as friends of template classes and the implications of doing so. Examples and error cases to help clarify the concepts.
Typology: Slides
1 / 26
This page cannot be seen from the preview
Don't miss anything!



















void doSomething( B< char >& ); class A { … }; template< class T > class B { int data; friend void doSomething( B
void doSomething( B< char >& cb ) { B< int > ib; ib.data = 5; // OK cb.data = 6; // OK }
template< class U > void doSomething( U ); template< class V > class A { … }; template< class T > class B { int data; friend void doSomething( T ); friend A< T >; };
int main() { int i = 5; char c = ‘x’; doSomething( i ); // OK doSomething( c ); // OK return 0; }
template< class U > void doSomething( U u ) { B< int > ib; ib.data = 78; }
class B< int > { int data; friend void doSomething( int ); friend A< int >; };
template< class T > class A { void method() { // Error! B< char > cb; cb.data = 8; B< int > ib; ib.data = 9; } };
template< class U > void doSomething( U ); template< class V > class A { … }; template< class T > class B { int data; template< class W > friend void doSomething( W ); template< class S > friend class A; };
template< class U > void doSomething( U u ) { B< int > ib; ib.data = 78; }
template< class T > class A { void method() { // OK! B< char > cb; cb.data = 8; B< int > ib; ib.data = 9; } };