Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Templates and Friends in Object-Oriented Programming (OOP) of C++, Slides of Object Oriented Programming

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

2011/2012

Uploaded on 11/09/2012

bacha
bacha 🇮🇳

4.3

(41)

215 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object-Oriented Programming
(OOP)
Lecture No. 38
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Understanding Templates and Friends in Object-Oriented Programming (OOP) of C++ and more Slides Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming

(OOP)

Lecture No. 38

Templates and Friends

  • Like inheritance, templates or their

specializations are compatible with

friendship feature of C++

void doSomething( B< char >& ); class A { … }; template< class T > class B { int data; friend void doSomething( B& ); friend A; … };

void doSomething( B< char >& cb ) { B< int > ib; ib.data = 5; // OK cb.data = 6; // OK }

Templates 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

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; }

  • Because doSomething() always

instantiates B< int >

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; } };

Templates 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