简体   繁体   English

如何在一个 function 中返回两个 char* class 变量?

[英]How can I return two char* class variables in one function?

I have the following class definition and the GetName function is supposed to return the full name(first and last name), which are char*.我有以下 class 定义和 GetName function 应该返回全名(名字和姓氏),它们是 char*。 Someone told me to concatenate them into a global variable and then to return it inside the function but I just can't figure out how to do that.有人告诉我将它们连接成一个全局变量,然后在 function 中返回它,但我就是不知道该怎么做。

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();
};

Yes, you can return two arrays:是的,您可以返回两个 arrays:

auto get() {
  std::array<std::array<char, 256>, 2> names;
  // ...

  return names;
}

Alternatively, you can create a name object and return that:或者,您可以创建一个名称 object 并返回:

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.最后,您可以考虑使用std::string代替 arrays,因为它更灵活。

Return result as std::pairstd::string,std::string result= std::make_pair(firstname, secondname);返回结果为 std::pairstd::string,std::string result= std::make_pair(firstname, secondname);

Consider also using structured binding to use the result of your function还考虑使用结构化绑定来使用 function 的结果

auto[first,second]=Getname()自动[第一,第二] =获取名称()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM