I have the following class definition. The GetName() function is supposed to return the full name (first and last name), which are char*. Someone told me to concatenate them into a global variable and then return it from the function, but I just can't figure out how to do that.
Can someone show me an example please?
class bank_Database
{
int AccountNumber;
std::string CNP, PhoneNumber;
char* First_Name[256], Last_Name[256];
float MoneyBalance;
public:
bank_Database() {};
void AddAccount(); //function to create an account
void ModifyDetails(); //function to modify name, phone and balance.
void DisplayAccDetails(); //function to display details
char* GetName(); //functions to get details
const std::string Get_PhoneNumber();
const int Get_AccNumber();
const std::string Get_CNP();
};
CodePudding user response:
Return result as
std::pair<std::string,std::string> result= std::make_pair(firstname, secondname);
Consider also using structured binding to use the result of your function
auto[first,second]=Getname()
If you really need char* that can be returned in pair or you can get from string with c_str function.
CodePudding user response:
Yes, you can return two arrays:
auto get() {
std::array<std::array<char, 256>, 2> names;
// ...
return names;
}
Alternatively, you can create a name object and return that:
struct Name
{
std::array<char, 256> first;
std::array<char, 256> last;
};
auto get()
{
Name name;
// ...
return name;
}
Finally, you may consider using std::string instead of arrays as it is more flexibly.
