Title: Computer Notes - Templates and Friends
1Templates and Friends
http//ecomputernotes.com
2Templates and Friends
- Like inheritance, templates or their
specializations are compatible with friendship
feature of C
http//ecomputernotes.com
3Templates and Friends Rule 1
- When an ordinary function or class is declared as
friend of a class template then it becomes friend
of each instantiation of that template
http//ecomputernotes.com
4Templates and Friends Rule 1
- void doSomething( Blt char gt )
- class A
- templatelt class T gt class B
- int data
- friend void doSomething( Bltchargt )
- friend A
-
http//ecomputernotes.com
5Templates and Friends Rule 1
- void doSomething( Blt char gt cb )
- Blt int gt ib
- ib.data 5 // OK
- cb.data 6 // OK
-
http//ecomputernotes.com
6Templates and Friends Rule 1
- class A
- void method()
- Blt int gt ib
- Blt char gt cb
- ib.data 5 // OK
- cb.data 6 // OK
-
http//ecomputernotes.com
7Templates and Friends Rule 2
- When a friend function / class template is
instantiated with the type parameters of class
template granting friendship then its
instantiation for a specific type is a friend of
that class template instantiation for that
particular type
http//ecomputernotes.com
8Templates and Friends Rule 2
- templatelt class U gt
- void doSomething( U )
- templatelt class V gt
- class A
- templatelt class T gt class B
- int data
- friend void doSomething( T )
- friend Alt T gt
http//ecomputernotes.com
9Templates and Friends Rule 2
- templatelt class U gt
- void doSomething( U u )
- Blt U gt ib
- ib.data 78
http//ecomputernotes.com
10Templates and Friends Rule 2
- int main()
- int i 5
- char c x
- doSomething( i ) // OK
- doSomething( c ) // OK
- return 0
-
http//ecomputernotes.com
11Templates and Friends Rule 2
- templatelt class U gt
- void doSomething( U u )
- Blt int gt ib
- ib.data 78
http//ecomputernotes.com
12Templates and Friends Rule 2
- int main()
- int i 5
- char c x
- doSomething( i ) // OK
- doSomething( c ) // Error!
- return 0
http//ecomputernotes.com
13Templates and Friends Rule 2
- Because doSomething() always instantiates Blt int
gt - class Blt int gt
- int data
- friend void doSomething( int )
- friend Alt int gt
http//ecomputernotes.com
14Templates and Friends Rule 2
- templatelt class T gt
- class A
- void method() // Error!
- Blt char gt cb
- cb.data 8
- Blt int gt ib
- ib.data 9
-
http//ecomputernotes.com
15Templates and Friends Rule 3
- When a friend function / class template takes
different type parameters from the class template
granting friendship then its each instantiation
is a friend of each instantiation of the class
template granting friendship
http//ecomputernotes.com
16Templates and Friends Rule 3
- templatelt class U gt
- void doSomething( U )
- templatelt class V gt
- class A
- templatelt class T gt class B
- int data
- templatelt class W gt
- friend void doSomething( W )
- templatelt class S gt
- friend class A
http//ecomputernotes.com
17Templates and Friends Rule 3
- templatelt class U gt
- void doSomething( U u )
- Blt int gt ib
- ib.data 78
-
http//ecomputernotes.com
18Templates and Friends Rule 3
- int main()
- int i 5
- char c x
- doSomething( i ) // OK
- doSomething( c ) // OK
- return 0
-
http//ecomputernotes.com
19Templates and Friends Rule 3
- templatelt class T gt
- class A
- void method() // OK!
- Blt char gt cb
- cb.data 8
- Blt int gt ib
- ib.data 9
-
http//ecomputernotes.com
20Templates and Friends Rule 4
- Declaring a template as friend implies that all
kinds of its specializations explicit, implicit
and partial, are also friends of the class
granting friendship
http//ecomputernotes.com
21Templates and Friends Rule 4
- templatelt class T gt
- class B
- T data
- templatelt class U gt
- friend class A
-
http//ecomputernotes.com
22Templates and Friends Rule 4
- templatelt class U gt
- class A
- A()
- Blt int gt ib
- ib.data 10 // OK
-
23Templates and Friends Rule 4
- templatelt class U gt
- class Alt U gt
- A()
- Blt int gt ib
- ib.data 10 // OK
-
-
http//ecomputernotes.com
24Templates and Friends Rule 4
- templatelt class T gt
- class B
- T data
- templatelt class U gt
- friend void doSomething( U )
-
25Templates and Friends Rule 4
- templatelt class U gt
- void doSomething( U u )
- Blt int gt ib
- ib.data 56 // OK
http//ecomputernotes.com
26Templates and Friends Rule 4
- templatelt gt
- void doSomethinglt char gt( char u )
- Blt int gt ib
- ib.data 56 // OK
-
http//ecomputernotes.com