当我离开该方法时,来自静态向量的数据保持不变

Data from a static vector left unchanged when I leave the method

这是我在学习 C++ 时想到的一个 Bank 项目,我 在我学习继承和指针时一直在添加它。这 用户是可以创建新客户或流程的银行出纳员 现有客户的交易。

A CustomerSavingsChecking class 是继承的私有字段 来自 Account。来自其私有 static vector<Customer> 字段的 Bank class adds/gets 客户。

意想不到的结果是在'process_transaction'方法中我可以获取 vector 中的客户和它的帐户中的 deposit/withdrawl,但是一旦我离开该方法并返回到它,帐户数据从我初始化它们时就没有改变。

需要帮助。这是客户问题的参考?我什么时候 return 引用还是指针?

这是代码。在驱动程序中 class 我有一个创建客户的方法

/*
* Create customer from teller input then add the Customer 
* to Bank's private static vector<Customer> field.
*/
int create_customer(){
    cout << "** Create New Customer **\n";
    cout << "Customer Name: ";
    string n = "";
    cin >> n;
    Customer* d = new Customer(n, gen_acct_number());
    Customer c(*d);
    // get opening balances
    double open_ck = 0;
    double open_sv = 0;
    cout << "Opening Balance For Checking: ";
    cin >> open_ck;

    cout << "Opening Balance For Savings: ";
    cin >> open_sv;
    cout << "\n";

    // create and set accounts
    Checking ck(open_ck);
    Savings sv(open_sv);
    c.set_checking(ck);
    c.set_savings(sv);

    // add customer to bank
    Bank b;
    b.add_customer(c);
    cout << "New Customer: " << c.get_name() << endl;
    cout << "Account Number: " << c.get_acct_number() << endl;
    cout << "\n";
    return 0;

}

我在驱动程序中有另一种方法 class 来处理客户。有一次,我 保留此方法客户的帐户不变,但否则有效。

/*
* Fetch customer by account number from  Bank's private static 
* vector<Customer> and perform transactions on Customer's accounts
* until teller has finished processing the customer.
*/
int process_customer(){
    cout << "** Process Transaction **\n";
    cout << "Account Number: ";
    int acctNum = 0;
    cin >> acctNum;
    cout << "\n";

    // get customer
    Bank b;
    Customer c = b.get_customer(acctNum);

    //if(c* == NULL){
    //  cout << "Error: Customer Not Found.\n";
    //  return 0;
    //}

    bool flag = true;
    while(flag){
        cout << c.get_name() << " #" << c.get_acct_number() << ": ";

        cout << "Select a transaction.\n";
        cout << "1. Withdrawl\n";
        cout << "2. Deposit\n";
        cout << "3. Check Balance\n";
        cout << "4. Quit\n";
        cout << "> ";
        int choice = 0;
        cin >> choice;
        cout << "\n";

        double amt = 0;
        int which = 0;
        switch(choice){
        case 1:{    // WITHDRAWL

            cout << "Withdrawl From: \n";
            cout << "1. Checking \n2. Savings \n";
            cout << "> ";
            cin >> which;
            cout << "\n";
            cout << "Amount: ";
            cin >> amt;
            cout << "\n";

            if(which == 1){
                cout << "Old Balance: " << c.get_checking().get_balance() << endl;
                c.get_checking().withdrawl(amt);
                cout << "New Balance: " << c.get_checking().get_balance() << endl;
                cout << "\n";
            }else if (which == 2){
                cout << "Old Balance: " << c.get_savings().get_balance() << endl;
                c.get_savings().withdrawl(amt);
                cout << "New Balance: " << c.get_savings().get_balance() << endl;
                cout << "\n";
            }else{
                break;
            }
            break;
        }
        case 2:{    // DEPOSIT

            cout << "Deposit Into: \n";
            cout << "1. Checking \n2. Savings \n";
            cout << "> ";
            cin >> which;
            cout << "\n";

            cout << "Amount: ";
            cin >> amt;
            cout << "\n";
            if(which == 1){
                cout << "Old Balance: " << c.get_checking().get_balance() << endl;
                c.get_checking().deposit(amt);
                cout << "New Balance: " << c.get_checking().get_balance() << endl;
                cout << "\n";

            }else if (which == 2){
                cout << "Old Balance: " << c.get_savings().get_balance() << endl;
                c.get_savings().deposit(amt);
                cout << "New Balance: " << c.get_savings().get_balance() << endl;
                cout << "\n";
            }else{
                break;
            }
            break;
        }
        case 3:{    // CHECK BALANCE

            cout << "Checking " << c.get_checking().get_balance() << endl;
            cout << "Savings  " << c.get_savings().get_balance() << endl;
            cout << "\n";
            break;
        }
        default:{   // EXIT

            flag = false;
            break;
        }
        }
    }

    return 0;
}

银行class.

Bank::Bank(){}

Customer& Bank::get_customer(int acct_number) {

    for(unsigned i = 0; i < cus.size(); i++){
        if(cus[i].get_acct_number() == acct_number){
            return cus[i];
        }
    }
    return cus[0];
}
void Bank::add_customer(Customer c){
    cus.push_back(c);
}

/* Disabled. I want to pass an account to these methods.
* 
* void Bank::deposit(double amt, Account& a){
*   a.deposit(amt);
* }
* void Bank::withdrawl(double amt, Account& a){
*   a.withdrawl(amt);
* }
* double Bank::check_balance( Account& a){
*   return a.get_balance();
* }
*/


vector<Customer> Bank::cus;

Bank.h

#ifndef BANK_H_
#define BANK_H_


#include "../include/Customer.h"
#include <string>
#include <vector>
using namespace std;

class Bank{

public:
    Bank();
    Customer& get_customer(int acct_number);
    void add_customer(Customer c);
    void deposit(double amt,  Account& a);
    void withdrawl(double amt,  Account& a);
    double check_balance( Account& a);
private:
    static vector<Customer> cus;
};



#endif /* BANK_H_ */

这确实是一个参考问题。在您的 process_customer 函数中,更改:

// get customer
Bank b;
Customer c = b.get_customer(acctNum);

至:

// get customer
Bank b;
Customer& c = b.get_customer(acctNum);

如果不进行此更改,您将复制客户然后修改此副本,而不是修改原始客户。