Reputation: 11389
I have a function that I pass a pointer to a vector of unsigned char.
Can somebody please tell me how to get one of the values inside the function?
double CApp::GetCost(unsigned char *uBytes)
{
unsigned char iHP;
iHP=uBytes[49]; //this does not work
}
Edit: Sorry, I first thought that I should simplify my code, but I think too much can go wrong. Now here is the real declaration:
// ---------------------------------------
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;
}
And this is how I use it:
dblTargetCost = GetCost(m_ByteFeatures.Content()[iUnitID].Features);
Another question: Would it be bad to simply pass the vector like this?
double CApp::GetCost(vector<unsigned char> &uBytes)
{
//...
}
Upvotes: 2
Views: 2723
Reputation: 45410
Would it be bad to simply pass the vector like this?
double CApp::GetCost(vector<unsigned char> &uBytes)
Not it's the better way to pass it by reference. however you may want to add const qualifier if you don't want uBytes
to be modified.
double CApp::GetCost(const vector<unsigned char> &uBytes)
{
try
{
unsigned char iHP = uBytes.at(49);
//...
}
catch(std::exception& e)
{
// process e
}
//...
}
EDIT:
After you new post, I feel you only need to return a reference to the element of m_content then pass the reference to GetCost
function
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;
}
then you call:
GetCost(m_ByteFeatures[iUnitID]);
Upvotes: 4