- 浏览: 408578 次
- 性别:
- 来自: 上海
-
文章分类
- 全部博客 (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)
最新评论
class member template can be useful in many situation.
The good side of member template is that there could be infinite set of number of nested classes CL and potential infinite number of member function assign().
/** * file * member_templates.h * description: * this is a file that demonstrate the use of the member template inside template classes */ #include <iostream> #include <string> using std::string; using std::ostream; using std::istream; using std::cerr; using std::cout; using std::endl; using std::exit; template<class Type> class QueueItem { public: QueueItem(const Type &val) : next(0), value (val) {} //~QueueItem() ; // Type value() { return this->value; } // QueueItem<Type> next() { return this->next; } //private: QueueItem<Type> *next; Type value; protected: }; template <class Type> class Queue { public: Queue() : front(0), back (0) {} ~Queue() ; Type remove(); void add(const Type &); bool is_empty() const { return front == 0; } // class member template // with this you can define // things like Queue<int>::CL<string> ... // which may sound useless at the frist glance, but we will see template <class T> class CL { T name; Type mem; }; template <class Iter> void assign(Iter first, Iter last) { while (!is_empty()) remove(); // call Queue<T>::remove for (; first != last; ++first) add(*first); } private: QueueItem<Type> *front; QueueItem<Type> *back; protected: }; // if it does not have a good destructor, then do not provide one. //template <class Type> //QueueItem<Type>::~QueueItem() //{ // //} template <class Type> Queue<Type>::~Queue() { while (!is_empty()) { remove(); } } template <class Type> void Queue<Type>::add(const Type &val) { QueueItem<Type> *pt = new QueueItem<Type>(val); if (is_empty()) { front = back = pt; } else { back->next = pt; } } template <class Type> Type Queue<Type>::remove() { if (is_empty()) { cerr << "remove() on empty queue\n"; exit(-1); } QueueItem<Type> *pt = front; front = front->next; Type retval = pt->value; delete pt; return retval; }
and belowis the implementeation code as below.
/** * file * member_templates.cppp * description: * this is a file that demonstrate the use of the member template inside template classes */ #include "member_templates.h" #include <vector> #include <iostream> #include <string> #include <list> using std::vector; using std::cout; using std::string; using std::list; void demo_class_templates() { Queue<int> qi; vector<int> vsi; qi.assign(vsi.begin(), vsi.end()); list<int> lp; qi.assign(lp.begin(), lp.end()); list<int*> lvp; //qi.assign(lvp.begin(), lvp.end()); }
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15382首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1047RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 797It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 1003As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1089typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 813There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 856we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 1033As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 781There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 985Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 1005As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 1022As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1367in this post, we are going to e ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 1009template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1232There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 885the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 609The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1195In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 899A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1234You can overload the subscript ...
相关推荐
- Enable use of processor specific built-in functions (mmmx, sse, sse2, pni, 3dnow) * "Default" button in Compiler Options is back * Error messages parsing improved * Bug fixes Version 4.9.8.5 * ...
- **Class Template Argument Deduction:** Use CTAD (Class Template Argument Deduction) to make template class instantiation more concise. - **Designated Initializers:** Use designated initializers for ...
Header Files The #define Guard Header File Dependencies Inline Functions The -inl.h Files Function Parameter Ordering Names and Order of Includes Scoping Namespaces Nested Classes Nonmember, Static ...
Standard Template Library STL Basics Class List-A Set of Students Creating a Waiting List with the STL List Storing Grades in a STL Map Putting It All Together Practical Considerations When Using the...
可重载声明(Overloadable declarations)、声明匹配(Declaration matching)、重载解析(Overload resolution)、重载函数的地址(Address of overloaded function)、重载运算符(Overloaded operators)、内建......
- 示例:`MAX_WIDTH`, `template<typename T> class Vector`. **原则1.5 关于指针标识符名(供参考)** - 指针变量名后可以加上 `_ptr` 或 `_p` 来表示该变量是指针类型。 - 示例:`char *text_ptr`. **原则1.6 ...
“带有Default Constructor”的Member Class Object “带有Default Constructor”的Base Class “带有一个Virual Function”的Class “带有一个virual Base class”的Class 总结 2.2 Copy Constructor的建构操作 ...
- 讨论了在C++中如何通过类(class)来定义对象(object),以及如何使用构造函数(constructor)和析构函数(destructor)管理对象的生命周期。 - 介绍了如何使用继承(inheritance)来扩展类的功能,并探讨了多态性...
model “has-a” or “is-implemented-in-terms-of” through composition. 条款39:明智而审慎地使用private继承 use private inheritance judiciously. 条款40:明智而审慎地使用多重继承 use multiple ...
- **空基类优化(Empty Base Class Optimization, EBBO)**:编译器优化技术之一,用于减少继承空基类的开销。 综上所述,《C++ Templates全覽》不仅覆盖了模板的基础知识,还深入探讨了许多高级主题和技术细节,是一...
C++ Standard Template Library (STL) 是 C++ 标准库的一部分,它提供了一系列高效的数据结构和算法,极大地提高了程序开发效率。本讲义旨在简练地介绍 STL 的核心概念和技术要点。 #### 二、模板与STL 模板是 C++ ...
Assuming no familiarity with C++, or any other C-based language, you’ll be taught everything you need to know in a logical progression of small lessons that you can work through as quickly or as ...
[template-spec] class [ms-decl-spec] [tag [: base-list ]] { member-list } [declarators]; [ class ] tag declarators; 参数 template-spec 可选模板说明。 ms-decl-spec 可选存储类说明有关更多信息 tag 给...
- **模板名** (`template-name`):由标识符表示。 值得注意的是,在类型定义中命名的类也可以作为类名 (_class.name_) 使用。 #### 三、词法规则 [gram.lex] 词法约定是C++语言的基础,包括关键字、标识符、文字...
- **模板成员函数(Template Member Functions)**:类模板中的成员函数也可以是模板化的,进一步增强了类的功能性和适应性。 - **模板特化(Template Specialization)**:允许为特定类型提供不同的实现,从而...