有没有一种方法可以将std :: make_unique与指定给函数的不同类一起传递 [英] Is there a way to pass std::make_unique with different classes specified into a function

查看:60
本文介绍了有没有一种方法可以将std :: make_unique与指定给函数的不同类一起传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个示例代码.这里总共使用了4个类似银行帐户的类,其中Account是一个抽象基类.CheckingAccount和SavingsAccount是从Account抽象类派生的,TrustAccount是从SavingsAccount派生的.我不会在这里详细介绍类的内部工作原理,因为它对问题没有任何意义.我想使用 try catch 以及从 std :: exception 派生的包含异常警告的类来实现简单的异常处理.我可以编写一个对n个对象执行此操作的函数,但前提是它们必须是Account的相同子类.但是,我想向向量添加3个不同的子类,但我想不出一种自动的方法.

So I've got this exemplary code. There are 4 bank account-like classes in total used here with Account being an Abstract base class. CheckingAccount and SavingsAccount are derived from the Account abstract class and the TrustAccount is derived from SavingsAccount. I won't go into detail about the inner workings of the classes here, as it is of no significance to the problem. I wanted to implement simple exception handling with try and catch and a class derived from std::exception containing the exception warning. I could write a function that does it for n objects, but only if they'd be the same subclass of Account. However, I want to add 3 different subclasses to the vector and I can't think of a way to automate it.

#include <iostream>
#include <memory>
#include <vector>
#include "checking_account.h"
#include "trust_account.h"
#include "account_util.h"

int main() {
    std::vector<std::unique_ptr<Account>> accounts{};    //A vector of unique_ptr to Account objects that I will be adding new Objects to
    std::unique_ptr<Account> 
    try {
        accounts.push_back(std::make_unique<CheckingAccount>("Joe", 200));
    }
    catch (const std::exception &ex) {
        std::cout << ex.what() << std::endl;
    }
    try {
        accounts.push_back(std::make_unique<TrustAccount>("John", -300, 0.1));
    }
    catch (const std::exception &ex) {
        std::cout << ex.what() << std::endl;
    }
    try {
        accounts.push_back(std::make_unique<SavingsAccount>("Jane", 150, 0.2));
    }
    catch (const std::exception &ex) {
        std::cout << ex.what() << std::endl;
    }
    return 0;
}

我想制作一个像这样的函数

I'd like to make a function looking like that

void create_account(std::vector<std::unique_ptr<Account>> &accounts, CLASS, CLASS_ARGS) {
    try {
        accounts.push_back(std::make_unique<CLASS>(CLASS_ARGS));
    }
    catch (const std::exception &ex) {
        std::cout << ex.what() << std::endl;
    }
}

但是我现在已经知道怎么看.有没有一种方法可以创建指向类的指针?(一个类不是一个类的对象,我认为有一种方法可以指向函数并将其作为参数传递,但我也不知道它是如何工作的.)

But I have now idea how could that even look. Is there a way to create a pointer to a class? (a class not an object of a class, I think there's a way to make a pointer to a function and pass it as a parameter, but I don't know how that works either).

推荐答案

使 create_account()为模板函数,例如:

template<typename T, typename... ArgTypes>
void create_account(std::vector<std::unique_ptr<Account>> &accounts, ArgTypes&&... args)
{
    try {
        accounts.push_back(std::make_unique<T>(std::forward<ArgTypes>(args)...));
    }
    catch (const std::exception &ex) {
        std::cout << ex.what() << std::endl;
    }
}

int main() {
    std::vector<std::unique_ptr<Account>> accounts;
    create_account<CheckingAccount>(accounts, "Joe", 200);
    create_account<TrustAccount>(accounts, "John", -300, 0.1);
    create_account<SavingsAccount>(accounts, "Jane", 150, 0.2);
    return 0;
}

这篇关于有没有一种方法可以将std :: make_unique与指定给函数的不同类一起传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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