简体   繁体   English

当函数返回类数据成员时如何返回其他内容?

[英]How I return something else when a function returns class data member?

AccountHolder Search(int accno, string CNIC, AccountHolder* customer, int counter) {
for (int i = 0;i < counter;i++) {
    if (account == customer[i].getAccno() && CNIC == customer[i].getCNIC()) {
        return customer[i];
    }
}
return (-----);
}

The problem is I want to return something which tells me that the account number and CNIC do not match with any customer id. 问题是我想返回一些告诉我帐号和CNIC与任何客户ID不匹配的信息。 I can do it by creating a new object and as my constructor sets all data members to zero or empty string, I may be able to check that such customer is not present but is there any other way? 我可以通过创建一个新对象来完成此操作,并且当我的构造函数将所有数据成员设置为零或空字符串时,我也许可以检查该客户是否存在,但是还有其他方法吗? It's my first question, so I hope i haven't asked a stupid question. 这是我的第一个问题,所以我希望我没有问过一个愚蠢的问题。 Thanks! 谢谢!

There are many ways of doing this, some are nicer than others but it depends on your situation. 这样做的方法很多,有些方法比其他方法更好,但这取决于您的情况。

If it is something that doesn't happen very often and is an actual error (this is key, as it is slow), throw an exception. 如果它很少发生并且是实际错误(这很关键,因为它很慢),则抛出异常。

You could instead return a const pointer to an object, and return NULL in the case of an error. 您可以改为返回指向对象的const指针,并在出现错误的情况下返回NULL。 This only makes sense if the original object is guarenteed not to disappear during its usage. 仅当保证原始对象在使用过程中不消失时,这才有意义。

You could instead return a bool and take your AccountHolder by reference and fill it in if there is a match. 您可以改为返回布尔值,并通过引用获取AccountHolder,如果匹配则填写。

You could also take a boolean by reference, I don't really like this option as its quite a strange interface choice. 您也可以通过引用获取布尔值,我不太喜欢这个选项,因为它是一个非常奇怪的界面选择。

I'm sure there are other choices. 我敢肯定还有其他选择。

I want to return something which tells me that the account number and CNIC do not match with any customer id 我想返回的内容告诉我帐号和CNIC与任何客户ID不匹配

Well there are two ways to consider this problem: 好,有两种方法可以考虑此问题:

  • use an exception 使用异常

if your Search() shall always have valid arguments (ie existing account number and CNIC), and never have wrong values, it shall fail loudly when it has a wrong set of arguments by throwing an exception: 如果您的Search()始终具有有效的参数(即现有帐号和CNIC),并且永远不具有错误的值,则当它具有一组错误的参数时,它会抛出异常,从而大声失败:

AccountHolder Search(int accno, string CNIC, AccountHolder* customer, int counter) {
    for (int i = 0;i < counter;i++) {
        if (account == customer[i].getAccno() && CNIC == customer[i].getCNIC()) {
            return customer[i];
        }
    }
    throw std::invalid_argument("Unknown account");
}

of course, a custom designed exception is even better. 当然,定制设计的异常甚至更好。

  • Use a singleton 使用单例

but if that situation is part of your design and this is not an error, but a feature, then it shall return a value that you can test against to tell you that the customer does not exist: 但是,如果这种情况是您设计的一部分,并且不是错误,而是功能,那么它将返回一个可以测试的值,以告诉您该客户不存在:

So you shall design your AccountHolder class with a custom field that you can check so you can create a global unknownAccountHolder instance that you return from this function and then can check against. 因此,您应使用可检查的自定义字段来设计AccountHolder类,以便可以创建从此函数返回并可以进行检查的全局unknownAccountHolder实例。 As you do not give your AccountHolder design. 由于您不提供AccountHolder设计。

class AccountHolder {
    static const AccountHolder unknownAccountHolder(-1, "invalid");
    […]
}

AccountHolder Search(int accno, string CNIC, AccountHolder* customer, int counter) {
    for (int i = 0;i < counter;i++) {
        if (account == customer[i].getAccno() && CNIC == customer[i].getCNIC()) {
            return customer[i];
        }
    }
    return AccountHolder::unknownAccountHolder;
}
  • use C style error handling is a wrong third option 使用C风格的错误处理是错误的第三种选择

you may use other options suggested to you: using a returned pointer, passing a boolean as parameter, returning a boolean and taking the object as argument… But that's all c-style coding and bad design as it's complicating your Search() function prototype and/or adding more memory management than necessary. 您可能会使用其他建议的选项:使用返回的指针,将布尔值作为参数传递,返回布尔值并将对象作为参数…但这都是c风格的编码和不良的设计,因为这会使Search()函数原型和/或添加不必要的更多内存管理。 You're doing C++ here, so try to keep it simple and use good OOP practice. 您在这里正在使用C ++,因此请尝试使其简单并使用良好的OOP做法。

pass a boolean result by reference as a parameter 通过引用将布尔结果作为参数传递
AccountHolder Search(int accno, string CNIC, AccountHolder* customer, int counter, bool &isMatch) AccountHolder搜索(int accno,字符串CNIC,AccountHolder *客户,int计数器,bool&isMatch)
{ {
/// ///
} }

Turn your return type into a pointer, or even better a smart pointer, and return NULL or an equivalent smart pointer: 将您的返回类型转换为指针,甚至更好的是智能指针,然后返回NULL或等效的智能指针:

AccountHolder * Search(int accno, string CNIC, AccountHolder* customer, int counter) {
  for (int i = 0;i < counter;i++) {
    if (account == customer[i].getAccno() && CNIC == customer[i].getCNIC()) {
      return &customer[i];
    }
  }
  return NULL;
}

In order to use smart pointers you'd have to change how customer is defined, eg by making it a std::vector<std::shared_ptr<AccountHolder> > . 为了使用智能指针,您必须更改定义客户的方式,例如,将其设为std::vector<std::shared_ptr<AccountHolder> > If your compiler doesn't provide std::shared_ptr you would need to use Boost . 如果您的编译器不提供std::shared_ptr ,则需要使用Boost

One option would be to return bool to indicate if the search is successful or not. 一种选择是返回bool以指示搜索是否成功。 In this case you would need an extra output parameter, which will store the result if the function returns true . 在这种情况下,您将需要一个额外的输出参数,如果函数返回true则该参数将存储结果。

bool Search(int accno, string CNIC, AccountHolder* customer, int counter, AccountHolder *result);

If you have boost handy, one thing worth considering is boost::optional<> . 如果方便使用boost,则值得考虑的一件事是boost::optional<> This is nice as it documents also exactly what the return could be. 很好,因为它也准确记录了回报。 You can return a boost::none , in the event that you don't have anything useful to return. 如果没有任何有用的返回值,则可以返回boost::none

暂无
暂无

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

相关问题 应该如何设置班级的char *成员? 使用strcat还是其他? - How should set a char* member of a class? Using strcat or something else? 当它是 class 数据成员时,如何调用指向成员 function 的指针? - How to invoke pointer to member function when it's a class data member? 如何从成员函数(通常)返回常量(返回到成员数据)返回常量 - How to return a constant from a member function that (usally) returns a reference (to member data) 我如何使用 function 返回成员数据的地址 - how do i return an address of a member data by using a function 如何从函数返回类成员的指针 - How to return a pointer of a class member from a function 如何在类中创建一个成员函数,该成员函数返回的值是该类对象的类型? - How do I create a member function in a class that returns a value that is the type of an object of that class? 函数返回类型:指针,参考或其他? - Function Return Type: Pointer, Reference or something else? 如何声明一个函数,该函数是类的成员,并返回指向线程的函数指针? - how to declare a function which is member of a class and returns a function pointer to a thread? 当我从类成员函数返回* this时,为什么要调用我的复制构造函数? - Why is my copy constructor being called when I return *this from a class member function? 如何编写一个返回仅存在于类中的类型的成员函数? - How do I write a member function that returns a type that only exists in the class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM