1

我有一个函数,我将指针传递给 unsigned char 向量。

有人可以告诉我如何获取函数内的值之一吗?

double CApp::GetCost(unsigned char *uBytes)
{
   unsigned char iHP;
   iHP=uBytes[49]; //this does not work
}

编辑:对不起,我首先认为我应该简化我的代码,但我认为太多可能会出错。现在这是真正的声明:

// ---------------------------------------
struct ByteFeature
{
    unsigned char Features[52];
};

class clsByteFeatures : public CBaseStructure
{
private:
   vector<ByteFeature> m_content;

protected:
   virtual void ProcessTxtLine(string line);

public:
   vector<ByteFeature> &Content();
   void Add(ByteFeature &bf);
};

vector<ByteFeature> &clsByteFeatures::Content()
{
   return m_content;
}

这就是我使用它的方式:

dblTargetCost  = GetCost(m_ByteFeatures.Content()[iUnitID].Features);

另一个问题:像这样简单地传递向量会很糟糕吗?

double CApp::GetCost(vector<unsigned char> &uBytes)
{
  //...
}
4

1 回答 1

3
Would it be bad to simply pass the vector like this?
double CApp::GetCost(vector<unsigned char> &uBytes)

不是通过引用传递它的更好方法。但是,如果您不想uBytes被修改,您可能需要添加 const 限定符。

double CApp::GetCost(const vector<unsigned char> &uBytes)
{
   try
   {
     unsigned char iHP = uBytes.at(49);
     //... 
   }
   catch(std::exception& e)
   {
     // process e
   }
   //...
}

编辑:

在您发布新帖子后,我觉得您只需返回对 m_content 元素的引用,然后将引用传递给GetCost函数

ByteFeature& clsByteFeatures::operator[](int i) { return m_content.at(i); }


double GetCost(const ByteFeature& bf)
{
    std::cout << bf.Features[49]; << std::endl;
    return 0.0;
}

然后你打电话:

GetCost(m_ByteFeatures[iUnitID]); 
于 2013-01-19T08:33:20.617 回答