Friend class or function and nested types in class templates
2011-11-16 19:28
806 查看
以下内容依据《C++ primer》编写。
1. Friend declaration in class templates
Three kinds of friend declarations
(1) A nontemplate friend class or friend function, they are friends to all instantiations of the class template
E.g.
(2) A bound friend class template or function template.
E.g.
template <class Type> class foobar{ ... };
template <class Type> void foo( QueueItem<Type> );
template <class Type>
class Queue {
void bar();
// ...
};
template <class Type>
class QueueItem {
friend class foobar<Type>;
friend void foo<Type>( QueueItem<Type> );
friend void Queue<Type>::bar();
// ...
};
Using this method, for every instantiation of QueueItem, the corresponding friend instantiation for the specified type is a friend to it. For instance, that is to say, foobar<int> is a friend to QueueItem<int> but not a friend to QueueItem.
(3) A unbound friend class template or function template.
E.g.
2. Nested Types of class templates
With one class being a nested private type, it becomes inaccessible to the general program except calling its enclosing class. The nested classes are automatically class templates. The mapping between an instantiation for the enclosing class template and
an instantiation of the nested class template is one to one. However, the nested class is not instantiated automatically when the enclosing class template is instantiated.
A member template can be defined outside its enclosing class. For example, the class member template CL or the member function template assign() can be defined as follows:
1. Friend declaration in class templates
Three kinds of friend declarations
(1) A nontemplate friend class or friend function, they are friends to all instantiations of the class template
E.g.
class Foo { void bar(); }; template <class T> class QueueItem { friend class foobar; friend void foo(); friend void Foo::bar(); // ... };
(2) A bound friend class template or function template.
E.g.
template <class Type> class foobar{ ... };
template <class Type> void foo( QueueItem<Type> );
template <class Type>
class Queue {
void bar();
// ...
};
template <class Type>
class QueueItem {
friend class foobar<Type>;
friend void foo<Type>( QueueItem<Type> );
friend void Queue<Type>::bar();
// ...
};
Using this method, for every instantiation of QueueItem, the corresponding friend instantiation for the specified type is a friend to it. For instance, that is to say, foobar<int> is a friend to QueueItem<int> but not a friend to QueueItem.
(3) A unbound friend class template or function template.
E.g.
template <class Type> class QueueItem { template <class T> friend class foobar; template <class T> friend void foo( QueueItem<T> ); template <class T> friend void Queue<T>::bar(); // ... };
2. Nested Types of class templates
With one class being a nested private type, it becomes inaccessible to the general program except calling its enclosing class. The nested classes are automatically class templates. The mapping between an instantiation for the enclosing class template and
an instantiation of the nested class template is one to one. However, the nested class is not instantiated automatically when the enclosing class template is instantiated.
template <class Type> class Queue { // ... private: class QueueItem { //nested class public: QueueItem( Type val ) : item( val ), next( 0 ) { ... } Type item; QueueItem *next; }; // because QueueItem is a nested type, // and not a template defined outside of Queue, // the template argument <Type> can be omitted // after QueueItem QueueItem *front, *back; // ... };
or
template <class T> //this allows a definition like Queue<int>::CL<char> class Queue { private: // class member template template <class Type> class CL { Type member; T mem; }; // ... public: // function member template template <class Iter> void assign( Iter first, Iter last ) { while ( ! is_empty() ) remove(); // calls Queue<T>::remove() for ( ; first != last; ++first ) add( *first ); // calls Queue<T>::add( const T & ) } };
A member template can be defined outside its enclosing class. For example, the class member template CL or the member function template assign() can be defined as follows:
template <class T> class Queue { private: template <class Type> class CL; // ... public: template <class Iter> void assign( Iter first, Iter last ); // ... }; template <class T> template <class Type> class Queue<T>::CL<Type> { Type member; T mem; }; template <class T> template <class Iter> void Queue<T>::assign( Iter first, Iter last ) { while ( ! is_empty() ) remove(); for ( ; first != last; ++first ) add( *first ); }
相关文章推荐
- functionclass[LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- C++中friend的使用(friend function and friend class)
- 解决1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and b
- Comparison between overloading operators in member function and in friend function
- C++11: default template arguments for both function and class templates
- functionclass[LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- This function has none of Deterministic,no sql,or reads sql data in its declaration and binary logging is enabled(you *might* want to use the less safe log_bin_trust_function_creators variable
- ReactJs 报错 Element type is invalid: expected a string (from built-in components) or a class/function (for composite components) but got: undefined. Check the render method of `Me`.
- Partial mock local private method or public method in the class and suppress static initial block
- 错误码: 1418 This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and
- [React] Define defaultProps and PropTypes as static methods in class component
- C++中friend的使用(friend function and friend class)
- expected a string (for built-in components) or a class/function (for composite components) but got:
- [C++] C++中friend的使用(friend function and friend class)
- C++中friend的使用(friend function and friend class) .
- C++中friend的使用(friend function and friend class)
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary log
- [C++]Partly Specialize member function in Class Templates
- Use Module and Function instead of Class in Python
- static member variable and static member function in a class