C++ OOP: Template Specialization, Inheritance, and Resolution Order, Slides of Object Oriented Programming

An in-depth exploration of template specialization, resolution order, function template overloading, and inheritance in object-oriented programming using c++. It covers the concept of template specialization, its derivations, and the resolution order of function calls and template instantiation. The document also explains the rules for using inheritance with templates and the differences between complete specializations, partial specializations, and ordinary classes.

Typology: Slides

2011/2012

Uploaded on 11/09/2012

bacha
bacha 🇮🇳

4.3

(41)

213 documents

1 / 33

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object-Oriented Programming
(OOP)
Lecture No. 37
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Partial preview of the text

Download C++ OOP: Template Specialization, Inheritance, and Resolution Order and more Slides Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming

(OOP)

Lecture No. 37

Resolution Order

template< typename T > class Vector { … };

template< typename T > class Vector< T* > { … };

template< > class Vector< char* > { … };

…Example – Resolution Order

int main() { Vector< char* > strVector; // Vector< char* > instantiated

Vector< int* > iPtrVector; // Vector< T* > instantiated

Vector< int > intVector; // Vector< T > instantiated return 0; }

Function Template Overloading

template< typename T > void sort( T );

template< typename T > void sort( Vector< T > & );

template< > void sort< Vector<char> >( Vector< char > & );

void sort( char* );

Example – Resolution Order

int main() { char* str = “Hello World!”; sort(str); // sort( char* )

Vector<char> v1 = {“ab”, “cd”, … }; sort(v1); //sort( Vector<char> & )

…Example – Resolution Order

Vector v2 = { 5, 10, 15, 20 }; sort(v2); // sort( Vector &)

int iArray[] = { 5, 2, 6 , 70 }; sort(iArray); // sort( T )

return 0; }

Derivations of a Template

  • A class template may inherit from another

class template

template< class T > class A { … };

template< class T > class B : public A< T > { … };

…Derivations of a Template

int main() { A< int > obj1; B< int > obj2; return 0; }

…Derivations of a Template

int main() { A< int > obj1; B< int* > obj2; return 0; }

…Derivations of a Template

  • Complete specialization or ordinary class

cannot inherit from a class template

template< > class B< char* > : public A< T > { … }; // Error: ‘T’ undefined

class B : public A< T > { … }; // Error: ‘T’ undefined

…Derivations of a Partial Sp.

template< class T > class B : public A< T* > { … }

int main() { A< int* > obj1; B< int > obj2; return 0; }

…Derivations of a Partial Sp.

  • A partial specialization may inherit from a

partial specialization

template< class T > class B< T* > : public A< T* > { … };

…Derivations of a Partial Sp.

  • Complete specialization or ordinary class

cannot inherit from a partial specialization

template< > class B< int* > : public A< T* > { … } // Error: Undefined ‘T’

class B : public A< T* > { … } // Error: Undefined ‘T’

Derivations of a Complete Sp.

  • A class template may inherit from a

complete specialization

template< class T > class A { … };

template< > class A< float* > { … };