1

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).

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982

1 Answers1

3

Make create_account() be a templated function, eg:

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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770