- 浏览: 389194 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 15284首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 964RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 744It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 919As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1031typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 735There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 783we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 913As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 721There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 930Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 924As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 950As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1286in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 903class member template can be us ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1180There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 800the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 553The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1133In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 837A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1126You 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 ...
In this book, author Ivor Horton explains what the class and function templates available with C++ 14 do, and how to use them in a practical context. You'll learn how to create containers, and how ...
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, ...
C++ Template C++ Template The complete Guide-examples
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
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 ...
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. ...
C++元编程参考 英文版计算机编程经典教材 适合学习软件构造的人参考
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 ...
Welcome to Using the C++ Standard Template Libraries. This book is a tutorial on the class and function templates that are contained within a subset of the header files that make up the C++ Standard ...
C++ template 中文版 候捷。 很好的学习C++ template 的资料。
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 ...
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 ...
C++Template just down load it !
The main reason for making a virtual function inline is to place its definition in the class, either for convenience or to document its behavior, e.g., for accessors and mutators. The -inl.h Files...
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 ...