C++模板——template_c++ template-CSDN博客
#include <QApplication>
#include <QDebug>
struct user_inform {
QString user_name;
QString address;
QString mobile;
int user_id;
};
struct goods_inform {
QString goods_name;
int price;
int goods_id;
int goods_num;
};
struct orders_inform {
QString user_name;
int orders_id;
int user_id;
};
template<typename T1>
bool get_table(QList<T1> &list){
if(typeid(T1)==typeid(user_inform)){
qDebug()<<"user_inform";
}
else if(typeid(T1)==typeid(goods_inform)){
qDebug()<<"goods_inform";
}
else if(typeid(T1)==typeid(orders_inform)){
qDebug()<<"orders_inform";
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QList<orders_inform> l;
get_table(l);
return a.exec();
}
情景:有三个自定义结构体。
我希望在函数get_table中,不同类型的结构体执行不同的操作。
如果是user_inform,执行操作A
如果是goods_inform,执行操作B
如果是orders_inform,执行操作C
#include <QApplication>
#include <QDebug>
struct user_inform {
QString user_name;
QString address;
QString mobile;
int user_id;
};
struct goods_inform {
QString goods_name;
int price;
int goods_id;
int goods_num;
};
struct orders_inform {
QString user_name;
int orders_id;
int user_id;
};
class M{
public:
template<typename T1>
bool get_table(T1 list){
if(typeid(T1)==typeid(user_inform)){
qDebug()<<"user_inform";
print(list);
}
else if(typeid(T1)==typeid(goods_inform)){
qDebug()<<"goods_inform";
print(list);
}
else if(typeid(T1)==typeid(orders_inform)){
qDebug()<<"orders_inform";
print(list);
}
}
void print(user_inform u)
{
qDebug()<<u.address;
}
void print(goods_inform u)
{
qDebug()<<u.goods_name;
}
void print(orders_inform u)
{
qDebug()<<u.user_name;
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
M m;
orders_inform inform;
inform.user_name="111";
m.get_table(inform);
goods_inform inform1;
inform1.goods_name="222";
m.get_table(inform1);
return a.exec();
}
orders_inform
"111"
goods_inform
"222"
#include <QApplication>
#include <QDebug>
struct user_inform {
QString user_name;
QString address;
QString mobile;
int user_id;
};
struct goods_inform {
QString goods_name;
int price;
int goods_id;
int goods_num;
};
struct orders_inform {
QString user_name;
int orders_id;
int user_id;
};
class M{
public:
template<typename T1>
bool get_table(QList<T1> &list){
if(typeid(T1)==typeid(user_inform)){
qDebug()<<"user_inform";
print(list);
}
else if(typeid(T1)==typeid(goods_inform)){
qDebug()<<"goods_inform";
print(list);
}
else if(typeid(T1)==typeid(orders_inform)){
qDebug()<<"orders_inform";
print(list);
}
}
void print(QList<user_inform> &u)
{
qDebug()<<u.at(0).address;
}
void print(QList<goods_inform> &u)
{
qDebug()<<u.at(0).goods_name;
}
void print(QList<orders_inform> &u)
{
qDebug()<<u.at(0).user_name;
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
M m;
QList<orders_inform> inform;
orders_inform inform2;
inform2.user_name="111";
inform<<inform2;
m.get_table(inform);
QList<goods_inform> inform1;
goods_inform inform3;
inform3.goods_name="222";
inform1<<inform3;
m.get_table(inform1);
return a.exec();
}
orders_inform
"111"
goods_inform
"222"