I have the following struct:
struct cBuffer
{
char data[1024];
int bytesRecorded;
bool flag;
cBuffer(char * data_, int bytesRecorded_, bool flag_) :
bytesRecorded(bytesRecorded_), flag(flag_)
{
memcpy(static_cast<void *>(data), static_cast<void *>(data_),
bytesRecorded);
}
};
I would like to turn char data[1024] to const char data[1024], the same goes
for the constructor:
cBuffer(const char * data_, int bytesRecorded_, bool flag_)...
But then I fail using the memcpy() function because of the wrong cast.
how could I sort that out?
thanks
Well your example compile and run fine... what is failling exactly?
Eric Pruneau
> bytesRecorded);
> }
> };
>
> I would like to turn char data[1024] to const char data[1024], the same
> goes for the constructor:
>
> cBuffer(const char * data_, int bytesRecorded_, bool flag_)...
>
> But then I fail using the memcpy() function because of the wrong cast.
>
> how could I sort that out?
>
> thanks
>
--
Yu Han
--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---
> I have the following struct:
no problem anymore. I have changed to this:
#include <algorithm>
struct buffer
{
char data[1024];
int bytesRecorded;
bool flag;
buffer(const char * data_, const int bytesRecorded_, const bool flag_) :
bytesRecorded(bytesRecorded_), flag(flag_)
{
std::copy(data_, data_ + (bytesRecorded_ * sizeof(char)), data);
}
};
thanks
That's wrong.
(OK, it doesn't actually matter here, because sizeof(char) is 1 by
definition, but your code is evidence of a number of misconceptions
which will bite you later:)
If bytesRecorded really is a count of bytes, you shouldn't be
multiplying it by sizeof(anything), because it's already a measure of
size.
If bytesRecorded is actually a badly-named count of objects, you still
shouldn't be multiplying it by sizeof(anything) because std::copy copies
objects, not bytes, and its arguments are iterators to objects, not
pointers to memory.
And you should be replacing data and bytesRecorded by a
std::vector<char>, anyway, and using its initializer to insert the
data..
>}
>};
>
>thanks
PS Most people who use the trailing-underscore convention do it the
other way round: member variables get the suffix, local variables and
parameters don't.
--
Richard Herring