I have some code like that
#define SIZE 10
Class User
{
public:
std::array<Account, SIZE> getListAccount()
{
return listAccount;
}
private:
std::array<Account, SIZE> listAccount
}
Class Account
{
public:
void setUserName(std::string newUSN)
{
userName=newUSN;
}
private:
string userName;
string password;
}
int main()
{
User xxx(.......);
xxx.getListAccount()[1].setUserName("abc"); // It doesn't effect
return 0;
}
Why doesn't the setUserName() function call in main change the name in my xxx User?
By the way:
std::array because I want to save data in binary file Return a reference to the list instead
std::array<Account, SIZE> & // << Note the &
User::getListAccount();
Or better, don't expose the internal structure
Account&
User::getUser(size_t n)
{
return listAccount[n];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With