2
一般来说,我做了以下内容:在C++ BLPAPI有没有办法避免使用hasElement或捕捉错误
if (msg.hasElement(BID_SIZE))
{
bid_sz=msg.getElementAsInt32(BID_SIZE);
}
如果我不使用检查首先使用hasElement - 那么字段不存在会扔
一般来说,我做了以下内容:在C++ BLPAPI有没有办法避免使用hasElement或捕捉错误
if (msg.hasElement(BID_SIZE))
{
bid_sz=msg.getElementAsInt32(BID_SIZE);
}
如果我不使用检查首先使用hasElement - 那么字段不存在会扔
而不是使用getElementAsInt32
使用getValueAs
的错误:
Element field;
int err=msg.asElement().getElement(&field, BID_SIZE);
if (!err)
{
int valerr=field.getValueAs(&bid_sz); // will call getValueAs for the type of bid
}
这将避免在消息中寻找BID_SIZE两次,而不会抛出w ^没有找到母鸡场。
另外,您可以通过各个领域的循环,并检查它是哪场:
Element asElem=msg.asElement();
size_t num=asElem.numElements();
for (size_t i=0; i < num; ++i)
{
Element field=asElem.getElement(i);
Name field_name=field.name();
if (field_name == BID_SIZE)
{
bid_sz=field.getValueAsInt32();
}
// check other fields
// put more likely fields at the top
}