`

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

    Google C++ Style Guide_英文版.pdf

    - **Class Template Argument Deduction:** Use CTAD (Class Template Argument Deduction) to make template class instantiation more concise. - **Designated Initializers:** Use designated initializers for ...

    C++Templates(中文版)

    - **Member Templates**:允许类中的成员函数和数据成员为模板,增加类的泛型能力。 #### 特殊设计与底层运作 此外,书中的章节还覆盖了以下特殊的设计模式和底层机制: - **Tuple**:一种组合多个不同类型数据的...

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

    《C++ Templates 全览》

    - **偏锋技巧**: 包括Friend Name Injection、Curiously Recurring Template Pattern (CRTP)、Template Template Parameters (TTPs)、Member Templates等。 - **特殊设计**: 如Tuple、Traits Templates、Expression ...

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

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

    C++ 标准 ISO 14882-2011

    可重载声明(Overloadable declarations)、声明匹配(Declaration matching)、重载解析(Overload resolution)、重载函数的地址(Address of overloaded function)、重载运算符(Overloaded operators)、内建......

    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++编码规范.

    - 示例:`MAX_WIDTH`, `template&lt;typename T&gt; class Vector`. **原则1.5 关于指针标识符名(供参考)** - 指针变量名后可以加上 `_ptr` 或 `_p` 来表示该变量是指针类型。 - 示例:`char *text_ptr`. **原则1.6 ...

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

    Based on the provided information from the file "Addison.Wesley.C++.by.Dissection.2002.pdf," we can derive a comprehensive overview of the C++ programming language as covered in this text. The book is...

    c++ templates 全览(侯捷)

    - 类模板:`template&lt;typename T&gt; class MyClass { ... };` **2. 模板参数:** - **类型参数**:如上例中的 `T`。 - **非类型参数**:`template&lt;int N&gt; struct MyStruct { ... };`。 **3. 参数推导:** - 编译器...

    数据结构与算法分析--C++描述(第3版)

    从给定的文件信息来看,虽然标题和描述指向的是“数据结构与算法分析”这本书,但实际内容却涉及另一本书《C++ Templates全览》,这本由侯捷等人翻译的书深入探讨了C++中的模板(Templates)概念,这是C++语言中用于...

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

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

    thinking in c++中文版

    - 讨论了在C++中如何通过类(class)来定义对象(object),以及如何使用构造函数(constructor)和析构函数(destructor)管理对象的生命周期。 - 介绍了如何使用继承(inheritance)来扩展类的功能,并探讨了多态性...

    Effective C++(第三版)

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

    C++ Templates(侯捷版1-10章).pdf

    - **空基类优化(Empty Base Class Optimization, EBBO)**:编译器优化技术之一,用于减少继承空基类的开销。 综上所述,《C++ Templates全覽》不仅覆盖了模板的基础知识,还深入探讨了许多高级主题和技术细节,是一...

    c++Stl讲义简练版

    C++ Standard Template Library (STL) 是 C++ 标准库的一部分,它提供了一系列高效的数据结构和算法,极大地提高了程序开发效率。本讲义旨在简练地介绍 STL 的核心概念和技术要点。 #### 二、模板与STL 模板是 C++ ...

Global site tag (gtag.js) - Google Analytics