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

Posted

技术标签:

【中文标题】当我离开该方法时,来自静态向量的数据保持不变【英文标题】:Data from a static vector left unchanged when I leave the method 【发布时间】:2016-12-30 09:50:11 【问题描述】:

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

Customer 具有 SavingsChecking 类作为继承的私有字段 来自AccountBank 类从其私有 static vector<Customer> 字段添加/获取客户。

意外的结果是在“process_transaction”方法中我可以获取 来自vector的客户并从其帐户中存款/取款,但是一旦我离开该方法并返回该方法,帐户数据从我初始化它们时起就没有改变。

需要帮助。这是对客户问题的参考吗?我应该什么时候回来 引用还是指针?

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

/*
* 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;


我在驱动程序类中有另一个方法来处理客户。有一次,我 保留此方法,客户的帐户不会更改,但可以使用其他方法。

/*
* 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;

银行类。

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_ */

【问题讨论】:

能看到cus的声明吗? (这是所有客户的容器,因此使用正确的变量名) vector&lt;Customer&gt; Bank::cus; 可能比您想象的要少 static。虽然没有.h文件,但谁能确定。 与您当前问题无关的评论:不要使用double(或其他浮点)类型作为金额(只要您不打算在实际应用中使用此代码,随意继续double 当然,只是不要让它溜进一些实际的生产)。 【参考方案1】:

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

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

到:

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

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

【讨论】:

谢谢,我知道了。由于需要返回未找到客户的值,是否需要通过指针返回客户? 现在您似乎在找不到帐号时返回cus[0]。因此,您确实需要返回一个指针,您只需检查您获得的客户是否是这个“默认客户 0”,例如通过在您的 process_customer 函数中查看他的帐号。如果您想通过nullptr 指示未找到的情况,那么是的,指针会更容易。 好的,使用指针。这是我的解决方案。我找到了一个匹配的客户并返回了&amp;cus[i](未找到返回一个空指针),我用Customer* resultPointer = ...得到了返回值,检查它不是空的,然后用Customer c = (*resultPointer);转换回一个值 不要做最后一点 (Customer c = (*resultPointer)),否则你又要重新修改副本了。要么始终坚持使用指针(这更有意义),要么(不那么明显,更奇怪),回到引用 Customer&amp; c = (*resultPointer) 你是对的,现在它无法修改客户。我修好了它,它按我的预期工作。谢谢

以上是关于当我离开该方法时,来自静态向量的数据保持不变的主要内容,如果未能解决你的问题,请参考以下文章

在 UITextField 中键入时保持占位符静态

静态类中的向量

来自类(c ++)的静态变量

当我从中调用非静态方法时,如何使处理程序静态以避免泄漏?

程序结构之静态本地变量

java 静态方法可以重载吗重写呢