template用法及常见错误

一,泛化基本数据类型

主要用于:不同的数据类型,相同的实现方法,可以用template封装成一个API。

实例

class Iprint
{
public:
    virtual void process(int value) =0;
    virtual void process(double value) =0;
    virtual ~Iprint(){}
};

class Print : public Iprint
{
public:
    Print()
    {
        cout<<"this is Print() construct"<<endl;
    }
    ~Print()
    {
        cout<<"this is Print() disconstruct"<<endl;
    }
    void process(int value)
    {
        cout<<"process int:"<<endl;
        PrintArray(value);
    }
    void process(double value)
    {
        cout<<"process double:"<<endl;
        PrintArray(value);
    }
    template<typename T>
    void PrintArray(T value);
};

template<typename T> void Print:: PrintArray(T value)
{
    cout<<"PrintArray value:"<<value<<endl;
} 

int main()
{

	Iprint *p = new Print();
	p->process(10);
	p->process(20.0);

	return 0;
}

二,泛化类

主要用于:不同的类,相同的成员函数/成员变量等,可以用template封装成一个对象。
有些情况和工厂模式类似。

实例

template<class T>
class Print
{
public:
    Print()
    {
        cout<<"this is Print() construct"<<endl;
    }
    
    void printType()
    {
        T t;
        t.process();
    }
};
class PrintA
{
public:
    PrintA()
    {
        cout<<"this is PrintA() construct"<<endl;
    }
    void process()
    {
        cout<<"PrintA::process "<<endl;
    }
};

class PrintB
{
public:
    PrintB()
    {
        cout<<"this is PrintB() construct "<<endl;
    }
    void process()
    {
        cout<<"PrintB::process "<<endl;
    }    
};


int main()
{
	Print<PrintA> print1;
	print1.printType();
    cout<<"***************************************"<<endl;	
	Print<PrintB> print2;
	print2.printType();
	return 0;
}

三,常见的问题

1,Missing template arguments before '.' token

原因:若类里包含template模板,该类实例时,不清楚对应的T是什么类型。
方案一:实例化时时指定类型,e.g:A<int> a;
方案一:可以用虚基类的方式,在派生类的方法里使用template

2, error: ‘T’ was not declared in this scope

原因:T没有声明
方案:在定义和声明时都加:template<typename T>

四,Typename由来

"typename"是一个C++程序设计语言中的关键字。相当用于泛型编程时是另一术语"class"的同义词。这个关键字用于指出模板声明(或定义)中的依赖的名称(dependent names)是类型名,而非变量名。

很多情况下typename和class用法一样

个人建议:typename用于基本类型,class用于class类型

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`MongoTemplate` 是 Spring Data MongoDB 提供的操作 MongoDB 数据库的 API,它提供了许多常见的数据库操作方法,其中包括更新数据的方法。 `MongoTemplate` 的 `update` 方法有多个重载形式,常用的有以下两种: 1. `updateFirst(Query query, Update update, Class<?> entityClass)`:更新符合查询条件的第一条数据。 2. `updateMulti(Query query, Update update, Class<?> entityClass)`:更新符合查询条件的所有数据。 其中,`query` 参数表示查询条件,可以使用 `Criteria` 类来构造查询条件;`update` 参数表示更新操作,可以使用 `Update` 类来构造更新操作;`entityClass` 参数表示要更新的实体类。 以下是一个简单的例子,演示如何使用 `MongoTemplate` 更新数据: ```java // 更新符合条件的第一条数据 Query query = Query.query(Criteria.where("name").is("John")); Update update = new Update().set("age", 30); mongoTemplate.updateFirst(query, update, User.class); // 更新符合条件的所有数据 Query query = Query.query(Criteria.where("age").lt(30)); Update update = new Update().inc("age", 1); mongoTemplate.updateMulti(query, update, User.class); ``` 上面的例子中,我们通过 `Query` 和 `Criteria` 构造了查询条件,然后使用 `Update` 构造了更新操作。在第一个例子中,我们使用 `updateFirst` 方法更新了符合条件的第一条数据,将其年龄设为 30。在第二个例子中,我们使用 `updateMulti` 方法更新了符合条件的所有数据,将它们的年龄增加了 1。 需要注意的是,更新操作是原子性的,如果更新过程中出现错误,所有的更新操作都将被回滚。此外,更新操作需要写入磁盘,因此可能会比查询操作慢一些。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值