`

c++ - member template in class template

    博客分类:
  • c++
c++ 
阅读更多

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());
}
 

 

分享到:
评论

相关推荐

    Bloodshed Dev-C++

    - 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 * ...

    Practical C++ Programming C++编程实践

    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...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    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 ...

    深度探索模C++对象模型PDF

    “带有Default Constructor”的Member Class Object “带有Default Constructor”的Base Class “带有一个Virual Function”的Class “带有一个virual Base class”的Class 总结 2.2 Copy Constructor的建构操作 ...

    深度探索C++对象模型 超清版

    “带有Default Constructor”的Member Class Object “带有Default Constructor”的Base Class “带有一个Virual Function”的Class “带有一个virual Base class”的Class 总结 2.2 Copy Constructor的建构操作 ...

    From C to C++

    // A C++ Program without class and object! #include using namespace std; const int N=200; void strUpper(char *s); void strLower(char *s); int main(){ char ms[N]; cout; cin.getline(ms,N); ...

    Effective C++(第三版)

    model “has-a” or “is-implemented-in-terms-of” through composition. 条款39:明智而审慎地使用private继承 use private inheritance judiciously. 条款40:明智而审慎地使用多重继承 use multiple ...

    《深度探索C++对象模型》(Stanley B·Lippman[美] 著,侯捷 译)

    “带有Default Constructor”的Member Class Object “带有Default Constructor”的Base Class “带有一个Virual Function”的Class “带有一个virual Base class”的Class 总结 2.2 Copy Constructor的建构操作 ...

    Exploring C++ 11

    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 ...

    C++中声明类的class与声明结构体的struct关键字详解

    [template-spec] class [ms-decl-spec] [tag [: base-list ]] { member-list } [declarators]; [ class ] tag declarators; 参数 template-spec 可选模板说明。 ms-decl-spec 可选存储类说明有关更多信息 tag 给...

    C++标准(Standard for Programming Language C++)

    21.4 Class template basic_string 608 21.5 Numeric Conversions 635 21.6 Null-terminated sequence utilities . 636 22 Localization library 640 22.1 General 640 22.2 Header &lt;locale&gt; synopsis 640 22.3 ...

    MICROSOFT FOUNDATION CLASS LIBRARY

    template is in Test.rc, which can be edited in Microsoft Visual C++. ///////////////////////////////////////////////////////////////////////////// Other standard files: StdAfx.h, StdAfx.cpp ...

    C++ 标准 ISO 14882-2011

    1.7 The C++ memory model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.8 The C++ object model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    一个跨平台的CString源码

    // the Standard C++ Library basic_string&lt;&gt; template and add to it the // the following conveniences: // - The full MFC CString set of functions (including implicit cast) // - writing to/reading ...

    千年封包解密库

    template is in SubPacket.rc, which can be edited in Microsoft Visual C++. Other standard files: StdAfx.h, StdAfx.cpp These files are used to build a precompiled header (PCH) file named ...

    Google C++ International Standard.pdf

    7.12 Pointer to member conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 7.13 Function pointer conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    Visual C++ 编程资源大全(英文源码 控件)

    Tute.zip How to get a button control wired-in and working(34KB)&lt;END&gt;&lt;br&gt;50,BetterBmpButton_src.zip An improvement on the CBitmapButton class.(22KB)&lt;END&gt;&lt;br&gt;51,hoverbutton.zip A simple drop-...

    Addison.Wesley.C++.by.Dissection.2002.pdf

    1.3 Implementing Our Algorithm in C++.... . . . 10 1.4 Software Engineering: Style...... . . 12 1.5 Common Programming Errors...... 13 1.6 Writing and Running a C++ Program.... . . . 14 1.6.1 ...

    C++标准库(第二版)英文版.pdf

    4.3.3 PassingExceptions with Classexception_ptr . . . . . . . . . . . . . . 52 4.3.4 Throwing Standard Exceptions . . . . . . . . . . . . . . . . . . . . . . . 53 4.3.5 Deriving from Standard ...

    DVR-MPEG4编码播放器

    template is in Mp4Player.rc, which can be edited in Microsoft Visual C++. Other standard files: StdAfx.h, StdAfx.cpp These files are used to build a precompiled header (PCH) file named Mp4...

Global site tag (gtag.js) - Google Analytics