-2

I am looking for a way to create a new instance of a class when a function is called, but still have access to the object inside other functions.

I thought about creating an instance of the class inside main() and writing over it from within a function, but it does not seem to do anything when the code is run.

This code is to imagine what I want accomplish.

#include <iostream>

class Account {
    private:
        int a;
    public:
        int b;
};

void createAccount(){
    // i want to create a class instance under certain conditions (function 
    //invoked)
    Account myAccount();
};

void getAccountInt(){
    //and access the newly created instance here in some way
    std::cout << myAccount.b << endl;
};

int main(){
    return 0;
}

I'm bad at asking these types of things, this is the best example i could come up with.

b1az3
  • 1
  • 1
  • 4
    When you need something like this, I you rather review the design and fix it. In doubt, pass it in parameters to your functions. – Guillaume Racicot Jun 18 '19 at 19:32
  • 2
    what is the actual problem you are trying to solve? why does `createAccount` not return the created instance? – 463035818_is_not_an_ai Jun 18 '19 at 19:33
  • You need to pass the object by reference from `main` to the function where you want to modify the object. See https://stackoverflow.com/questions/29156958/pass-by-value-and-pass-by-reference. – R Sahu Jun 18 '19 at 19:34
  • One way could be to create a [Singleton](https://stackoverflow.com/questions/17712001/how-is-meyers-implementation-of-a-singleton-actually-a-singleton), not that's really considered a good design though. – πάντα ῥεῖ Jun 18 '19 at 19:55
  • Props on trying to come up with enough code to illustrate your question, and making your own attempt at a solution, although it borders on tutorial level code. You'll get more out of an actual teaching book or class. – Kenny Ostrom Jun 18 '19 at 20:00
  • FWIW, `Account myAccount();` is a function declaration. – L. F. Jun 19 '19 at 10:10

2 Answers2

0

You can create a global instance in a deferred way by using a Scott Meyer's singleton. This will work but is a questionnable design:

auto global_account() -> Account& {
    static auto account = Account{};
    return account;
}

void create_account() {
    auto& account = global_account();
    // set account stuff
};

void get_account_int() {
    auto& account = global_account();
    std::cout << account.b << std::endl;
};

But a proper solution would be to pass the account to the function that needs one from your main or other function:

auto create_account() -> Account {
    auto account = Account{};
    // set account stuff
    return account;
};

void get_account_int(Account& account) {
    std::cout << account.b << std::endl;
};

auto main() -> int {
    auto account = create_account();
    auto acc_int = get_account_int(account);
}
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
0

Your function appears to be there to decide whether or not you want to create an account (baesd on your comments). In that case, it doesn't have to actually create it. You just need it to make a decision, which your code can react to.

#include <iostream>

class Account {
private:
    int b;
public:
    Account() : b(0) {};
    int getAccountInt() const { return b; }
};

bool shouldCreateAccount() {
    return true; // you handle the decision here
}

int main() {
    if (shouldCreateAccount()) {
        Account myAccount;
        std::cout << myAccount.getAccountInt() << std::endl;
    }
    return 0;
}

The more general principle is https://en.cppreference.com/w/cpp/language/raii If there is an Account object, it has to be valid. Note that if we decide to make an Account, we have one, and if we don't decide to make an Account, there is nothing there. This will make a lot more sense when you cover "scope."

My opinion on why your code wouldn't work is you tried to have a Account when there wasn't a valid Account to have, violating RAII. Although I do have a weird way of looking at things, sometimes.

Kenny Ostrom
  • 5,639
  • 2
  • 21
  • 30
  • Thanks, this is more or less what I was looking for. Scope is exactly my question, as I was trying to create a global variable from a local scope. Your example was good, and your theory worked for my purposes. Id upvote your answer, but I don't have enough reputation. – b1az3 Jun 19 '19 at 00:32