Jump to content

Problem in class method

- - - - -

  • Please log in to reply
4 replies to this topic

#1
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 176 posts
Hi, something is wrong with Bank class' method bank_insertAccount. When i run it through debuger and reach the last line of method with this sign }, program doesn't go back to main function, but calls ~Account.
Here's header:

#ifndef BANK3
#define BANK3

#include <list>

class Account
{
public:

Account();
~Account();
void account_addMoney(int);
void account_substractMoney(int);
bool account_setCode(char *);
char* account_getCode() const;
int account_getCodeLength() const;
bool account_setID(char *);
char* account_getID() const;
int account_getIDlength() const;
void account_print() const;
int account_getMoney() const;
bool operator< (const Account ) const;

private:

int cash;
char *code;
char *id;

protected:

static const int code_length = 4;
static const int id_length = 4;

friend class Bank;
};


class Bank
{
public:

Bank();
virtual ~Bank();
void bank_insertAccount(Account);
bool bank_removeAccount(char *);
bool bank_addMoney(char *, int);
bool bank_substractMoney(char *, int);
bool bank_printAccountInfo(char *);
int bank_getIDlength();
int bank_getCodeLength();
virtual bool bank_setBankName(char *) = 0;

private:

Account acc;
list<Account> account_list;
list<Account>::iterator it;

protected:

char *name;

};

class TheBank: public Bank
{
public:

bool bank_setBankName(char *);

};

#endif

Here's implementation of bank_insertAccount:
void Bank::bank_insertAccount(Account account)
{

account_list.push_front(account);
account_list.sort();
it = account_list.begin();
it -> account_print();

}
Any help? thanks in advance

#2
julmuri

julmuri

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts
void Bank::bank_insertAccount(Account account)
account is temporary and destructor will be called when it goes out of scope.
You need to use const reference and copy semantics.

void Bank::bank_insertAccount(const Account& account)
Your code will work though, cause list will make its own copy anyhow. ( after you create proper copy constructor && assignment operator)
std::string s("oberq zhpu?");std::for_each(s.begin(),s.end(),[&](char&c){c=~c;c=~c-0x01/(~(c|0x20)/0x0D*0x02-0x0B)*0x0D;});std::cout<<s;

#3
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 176 posts
So as i understood, in function bank_insertAccount i have to create another object of Account (Account account2) and copy to it contents of passed to function account (account2 = account), right?

#4
julmuri

julmuri

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts
You need to make Account class copyable && assignable.
Account( const Account& other )
    : cash( other.cash )
    , code( 0 )
    , id( 0 )
{
    //
    // alloc and copy code && id
}

Account& operator=( const Account& other )
{
    if ( this != & other )
    {
        cash = other.cash;
        //
        // alloc and copy code && id
    } // if

    return *this;
}
Compiler defines these for you if you dont,
but with pointers as data members you get undefined behaviour.
You should use std::string over raw char strings anyways.
std::string s("oberq zhpu?");std::for_each(s.begin(),s.end(),[&](char&c){c=~c;c=~c-0x01/(~(c|0x20)/0x0D*0x02-0x0B)*0x0D;});std::cout<<s;

#5
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 176 posts
Thanks a lot, problem is solved. Until know i didn't know anything about copy operators




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users