C++中的template与typeid

C++中判断数据的类型_c++判断数据类型-CSDN博客

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"

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lpl还在学习的路上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值