C++模板的使用例子(别看)

本文详细探讨了C++模板的使用,从基础的函数模板到类模板,深入讲解了模板的实例化、模板特化以及模板元编程。通过具体实例,帮助读者掌握模板在实际编程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <iostream>
#include <functional>

template <typename MsgT>
class Msg {
public:
	Msg(int size) : size_(size) { element_ = new MsgT[size]; }
	~Msg() { delete []element_; };
	const MsgT& element(int index) const;
	void set_element(int index, const MsgT& value);
	int size() const { return size_; };
	void PrintElement(int index) const { std::cout << element(index) << std::endl; }  //怎么去打印每个数组呢?count?
private:
	MsgT *element_;
	int size_;
};

template <typename MsgT>
const MsgT& Msg<MsgT>::element(int index) const {
	return  element_[index];
}

template <typename MsgT>
void Msg<MsgT>::set_element(int index, const MsgT& value) {
	element_[index] = value;
}

template <typename T>
int PublishMsg(const Msg<T> &msg) {
	//由于参数msg是const的,所以只能调用其const的成员函数,所以size()和PrintElement()要加const后缀(声明和定义都要加)。
	for (int i = 0; i < msg.size(); i++) {
		msg.PrintElement(i);

	}
	return 0;
}

template <typename T>
int PublishMsgWrapper(int cmd, Msg<T> *msg, int len) {
	std::cout << "cmd = " << cmd << ", len = " << len << std::endl;
	return PublishMsg(*msg);
}

template <typename T>
//std::function<int (const Msg<T> &msg)> SubHandler;
typedef int (*SubHandler)(const Msg<T> &msg); //typedef不能定义模板,报错:“int (__cdecl *__cdecl SubHandler)(const Msg<MsgT> &)”: 不能是模板定义
int Subscribe(const std::string& name, SubHandler *handler) {
	return 0;
}

int IntHandler(const Msg<int> &msg) {
	return 0;
}

int main() {
	Msg<int> a_msg(8); //或者 Msg<int> a_msg = Msg<int>(8);

	for (int i = 0; i < 8; i++) {
		a_msg.set_element(i, i * 2);
	}
	PublishMsgWrapper(59, &a_msg, 8);
	return 0;
}

#include <stdio.h>
#include <iostream>
#include <functional>

template <typename MsgT>
class Msg {
public:
  Msg(int size) : size_(size) {element_ = new MsgT[size]; }
  ~Msg() { delete []element_; };
  const MsgT& element(int index) const;
  void set_element(int index, const MsgT& value);
  int size() const { return size_; };
  void PrintElement(int index) const { std::cout << element(index) << std::endl; }
private:
  MsgT *element_;
  int size_;
};

template <typename MsgT>
const MsgT& Msg<MsgT>::element(int index) const {
    return element_[index];
}

template <typename MsgT>
void Msg<MsgT>::set_element(int index, const MsgT& value) {
    element_[index] = value;
}

//template <typename T>
//typedef std::function<int (const Msg<T> &msg)> SubHandler;

template <typename T>
int PublishMsg(const Msg<T> &msg) {
    for (int i = 0; i < msg.size(); i++) {
		msg.PrintElement(i);
    }
	return 0;
}

/* class或者struct都不允许只给数据成员设置模板,而只能给整个类或者某个函数成员。
而如果是整个类,则下面RouteMsgList的数据定义在一开始就要指定模板类型 */
class RouteMsgList {
public:
    template <typename T>
    RouteMsgList(int (*sub_handler)(const Msg<T> &msg), int set, int id) {
        cmdset_ = set;
        cmdid_ = id;
        Msg<int> a_msg(8);

        for (int i = 0; i < 8; i++) {
	    	a_msg.set_element(i, i * 2);
	    }

        PublishMsg(a_msg);

        SetSubHandler(a_msg, sub_handler);
    }
    int cmdset_;
    int cmdid_;
    //template <typename T>
    //std::function<int (const Msg<T> &msg)> sub_handler_;
    ///int SubHandler(const Msg<T> &msg);
};

template <typename T>
int SetSubHandler(const Msg<T> &msg, int (*sub_handler)(const Msg<T> &msg)) {
    printf("=>=>=>\n");
    return 0;
}

int IntHandler(const Msg<int> &msg) {
	return 0;
}

static RouteMsgList route_list[] = {
    RouteMsgList(IntHandler, 3, 1),
};



int main() {
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值