[英]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: 好,有两种方法可以考虑此问题:
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. 当然,定制设计的异常甚至更好。
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;
}
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.