C++ return different objects

前端 未结 3 1142
别跟我提以往
别跟我提以往 2021-01-29 12:30

i have a big problem.. I wonna select the Storage Service via a wrapper class. The returning value must be an object within the storage service class. I pasted my current approa

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-29 12:56

    in this code

    auto test(int select) {
        if( select == 1)
        {
            return new SQL(); 
        } else {
            return new REDIS();
        }
    

    auto can't be deduced because it only match to exact type. so even if SQL and REDIS inherite from StorageTemplate, StorageTemplate won't be deduced. you need to spécifie the type

    StorageTemplate* test(int select) {
        if( select == 1)
        {
            return new SQL(); 
        } else {
            return new REDIS();
        }
    

提交回复
热议问题