- 浏览: 399959 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
template class may defined their friends template function, friend function or even friend class to aid it provide some functions.
But normally how do you implements the with the friends?
Let's see the following examples on the Queue and the QueueItem classes
#include <iostream> #include <string> using std::istream; using std::ostream; template <class Type> class QueueItem; template <class Type> void foo(QueueItem<Type>); template <class Type> class Queue { public: friend void foo(Queue<Type>); // - this mean foo is a non-template function, it is just a normal function which accepts the parameter of type Queue<Type> // or you can write as follow friend void foo<Type>(Queue<Type>); // - this means that foo is a template function // whether we should write as template<class Type> // since we cannot do as friend ostream& operator << (ostream&, const Queue<Type> &);, we have to provide the templtae<class Type> is there better ways? friend ostream& operator <<(ostream& , const Queue<Type> &); // or as //friend operator <<(ostream&, const Queue<Type> &); // while you cannoot provide type parameter as the non-operator normal function template //friend ostream& operator << <Type>(ostream&, const Queue<Type> &); // this is a partial function instantiation //template <> friend ostream& operator << (ostream& , const Queue<Type> &); protected: virtual ostream& print(ostream&) const; private: QueueItem<Type> *front; QueueItem<Type> *back; }; template <class Type> class QueueItem { public: // all queue instantiation are friends // to each QueueItem instantiation //template <class T> friend class Queue; // it is not "friend template <class T> class Queue; friend class Queue<Type>; // //friend ostream& operator << <Type>(ostream &, const Queue<Type> &); // or you can write as follow template<class Type> friend ostream& operator << (ostream &, const QueueItem<Type> &); template<class Type> friend ostream& operator << (ostream &, const Queue<Type> &); //template <class T> friend ostream& operator << (ostream& , const QueueItem<T> &); operator Type () { return value; } // - the data accessor protected: virtual ostream& print(ostream &) const; private: QueueItem<Type> *next; Type value; }; // and if you want to define the helper ostream oeprator, how would you do that // you can make the function a template function // template <class Type> ostream& operator <<(ostream &, const Queue<Type> &); // and how is it implemented template <class Type> ostream& operator <<(ostream &os, const Queue<Type> & q) { os << "< "; QueueItem<Type>* p; for (p = q.front; p; p = p->next) { os << *p << " "; } os << "> "; return os; } template <class Type> ostream& operator <<(ostream& os, const QueueItem<Type> & q) { os << q.value; return os; }
- the code we have examine the
- friend template function
- friend function
- and friend classes
we also have examined the difference of writting
friend void foo<Type>(Queue<Type>);
and
friend void foo(Queue<Type>);
and we will see that why we need to write
template <class Type> friend ostream& operator <<(ostream& , Queue<Type>);
rather than
template <class Type> friend ostream& operator <<(ostream& , Queue<Type>);
But is that all, is that all we can do ?
what if there is derived class to Queue<T> or QueueItem<T> , here is the improved code
do you see the code , where we have defined the virtual print method.
virtual ostream& print(ostream&) const;
and no
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15342首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1013RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 772It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 964As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1060typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 773There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 824we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 979As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 754There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 960Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 976As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 994As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1330in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 939class member template can be us ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1212There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 855the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 589The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1170In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 875A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1188You can overload the subscript ...
相关推荐
on, three pioneering C++ experts show why, when, and how to use modern templates to build software that’s cleaner, faster, more efficient, and easier to maintain. Now extensively updated for the ...
Instead, youll learn how to write .NET applications using C++/CLI. <br>This book is based on its highly successful predecessor, and bridges the gap between classic C++ and C++/CLI. Furthermore, ...
Intermediate-level programmers who have learned a few advanced template techniques will see where these tricks fit in the big picture and will gain the conceptual foundation to use them with ...
on, three pioneering C++ experts show why, when, and how to use modern templates to build software that’s cleaner, faster, more efficient, and easier to maintain. Now extensively updated for the ...
The book provides comprehensive documentation of each library component, including an introduction to its purpose and design; clearly written explanations of complex concepts; the practical ...
Addison Wesley - C++ Template Metaprogramming Concepts Tools And Techniques From Boost And Beyond
C++11 and C++14 have made significant changes to improve not only a variety of libraries but also the core language. C++14 is the newest version of C++ which was released in August of 2014. ...
To make full use of its components - and to benefit from their power - you need a resource that does far more than list the classes and their functions. The C++ Standard Library - A Tutorial and ...
Chapter 12: Arrays and the vector Class Template Chapter 13: Multidimensional Arrays and Vectors Chapter 14: Building ClassesChapter 15: Pointers and Linked Structures Chapter 16: Data Structures ...
It details some of C++’s most powerful language elements, such as class types, templates and the STL, to develop components for microcontroller register access, low-level drivers, custom memory ...
Interesting, entertaining, and challenging exercises encourage students to make a difference and use computers and the Internet to work on problems. To keep readers up-to-date with leading-edge ...
To make full use of its components–and to benefit from their power–you need a resource that does far more than list the classes and their functions. The C++ Standard Library: A Tutorial and ...
1.3 A Prelude To Pattern Recognition 1.4 Statistical Pattern Recognition 1.5 Syntactic Pattern Recognition 1.6 The Character Recognition Problem 1.7 Organization Of Topics References And ...
C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond,《C++模板元编程》英文版。C++ In-Depth系列丛书之一,C++程序员必备。
Nevertheless, to support programmers who still use “old” C++ environments, this book will describe differences between C++ versions whenever they appear. I learned C++11 the hard way. Because I ...
In addition to The C++ Standard Library, a worldwide best-seller since its first publication in 1999, his books include C++ Templates: The Complete Guide (with David Vandevoorde and Doug Gregor, ...
C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond
The C++ Standard Library provides a set of common classes and interfaces that greatly extend the core C++ language. Josuttis' book not only provides comprehensive documentation of each library ...
Conversion from C++ to R and back is driven by the templates Rcpp::wrap and Rcpp::as which are highly flexible and extensible, as documented in the Rcpp-extending vignette. Rcpp also provides Rcpp ...