class WavFile
{
std::vector<short> SoundData;
public:
std::vector<short> getSoundData()
{
return SoundData;
}
}
Normally I’d use fread( &SoundData[0], 1, 1337, SomeFile );
But now that I’m using a vector inside a class, I’m having trouble with the following:
-
Knowing how to set the vector size outside the class.
-
And I’m not sure how to use the method as an argument for
freadto put the data inside the vector.
What you are trying to do is bad in general. There are some main points to it:
If you are encapsulating
vectorin class, you can still operate on it normally from the outside if it’s public.You can also wrap some of the calls to the vector,
.resize()for example:If you are using
std::vector, you should also use corresponding C++ read functions on it. Treatingvectoras a memory array is valid, but there are better ways to use it,fstreamfor example.If you are encapsulating
vector(or anystdcontainer in general), don’t return it by value! It will cause all of the elements to be copied. Instead, return a const-reference to container, or, even better, range.