C++ 返回不同的对象 [英] C++ return different objects

查看:36
本文介绍了C++ 返回不同的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大问题..我想通过包装类选择存储服务.返回值必须是存储服务类中的对象.我粘贴了我目前的方法.但到目前为止,我的心态并没有奏效.

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 approach. But my mindset didn't worked so far.

错误:

错误:自动返回类型推导不一致:‘SQL*’然后‘REDIS*’返回新的REDIS();

error: inconsistent deduction for auto return type: ‘SQL*’ and then ‘REDIS*’ return new REDIS();

最大的愿望是有一个接口类来定义结构和一些驱动程序类",其中包含目标存储服务的所有必要操作.

The big wish is to have an interface class which defines the struct and some "driver classes" which contains all necessary operations for the target storage service.

我希望你有另一种方法,我如何解决这个问题..

I hope you have another approach, how I can solve this problem..

    #include <iostream>


class StorageTemplate {
    public:
        virtual bool UserhasSurName() = 0;
        virtual bool UserhasGivenName() = 0;
};

class SQL: public StorageTemplate {
    public:
        bool UserhasSurName() {
            //A SQL QUERY
            return true;
        }
        bool UserhasGivenName() {
            //AN ANOTHER SQL QUERY
            return true;
        }
};

class REDIS: public StorageTemplate {
    public:
        bool UserhasSurName() {
            //A REDIS CALL
            return false;
        }
        bool UserhasGivenName() {
            //A REDIS CALL
            return false;
        }
};


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



int main(int argc, char const *argv[])
{
    controller cont;
    auto schnitzel = cont.test(1);
    auto mitzel = cont.test(2);
    std::cout << schnitzel->UserhasSurName() << std::endl;
    std::cout << mitzel->UserhasSurName() << std::endl;
}

推荐答案

你面临的问题如下:考虑你的功能

The problem you are facing is the following: Consider your function

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

如果您尝试评估 test(1) 这将扩展为

If you trying to evaluate test(1) this expands to

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

导致类型错误!

我向您展示了三种解决您问题的方法:

I show you three workarounds for your problem:

1.函数模板和if constexpr

使 test 成为一个函数模板,并使用 C++17 特性 if constexpr 来检查正确的类型:

Make test a function template and check for the correct type using the C++17 feature if constexpr:

template<typename T>
auto test() {
    if constexpr(std::is_same<T, SQL>::value) {
        return new SQL();
    } else {
        return new REDIS();
    }
}

像这样在 main() 中使用它:

Use it in main() like this:

int main(){
    controller cont;
    auto schnitzel = cont.test<SQL>();
    auto mitzel = cont.test<REDIS>();
    std::cout << schnitzel->UserhasSurName() << std::endl;
    std::cout << mitzel->UserhasSurName() << std::endl;
}

2.函数模板和 std::unique_ptr

如果您想避免使用 if constexpr,您可以简单地返回 std::unique_ptr 的实例而不是原始指针.这是首选方法:

If you want to avoid using the if constexpr you can simply return an instance of std::unique_ptr instead of a raw pointer. This is the preferred way to do:

template<typename T>
auto test() {
    return std::unique_ptr<T>(new T);
}

或者,您可以只返回 std::make_unique().

Alternatively you can just return std::make_unique<T>().

3.返回基类的一个实例

这是避免类型错误的最明显的解决方案:只返回基类的一个实例.如上所述,这里首选使用智能指针的解决方案:

This is is most obvious solution to avoid the type error: Just return an instance of the base class. As above a solution using smart pointers is preferred here:

std::unique_ptr<StorageTemplate> test(const int select) {
    if (select == 1) {
        return std::make_unique<SQL>();
    } else {
        return std::make_unique<REDIS>();
    }
}

如果你真的想避免使用智能指针,就使用像这样的原始指针:

If you really want to avoid using smart pointers just use raw ones like this:

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

这篇关于C++ 返回不同的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆