I currently have a class like this:
struct Rgb
{
static const int NUM_CHANNELS = 3;
unsigned char channel[NUM_CHANNELS];
};
which is never instantiated but is used to determine the stride of a pointer increment.
I wanted to allow this stride to change based on some runtime information. Will something like this work?
#include <iostream>
class Rgb
{
public:
static int* Channel;
static void SetChannelSize(unsigned int channelSize)
{
Channel = new int(channelSize);
}
};
int main()
{
int numbers[1000];
for(unsigned int i = 0; i < 1000; i++)
{
numbers[i] = i;
}
Rgb::SetChannelSize(3);
Rgb* rgbNnumbers = reinterpret_cast<Rgb*>(numbers);
for(unsigned int i = 0; i < 5; i++)
{
std::cout << rgbNnumbers[i] << std::endl; //error: no match for ‘operator<<’ in ‘std::cout << *(rgbNnumbers + ((unsigned int)i))’
}
return 0;
}
(I wasn’t sure how to step over the demo array I setup in this example to test it out).
Any comments?
As
[i]is a compile-time operation, it will simply not work for element sizes that are not known at compile-time. You must increment the pointer yourself with arithmetic.But that being said, most likely you actually know the size of the element at compile-time. You are probably writing specific code for 24-bit and 32-bit (et al) pixel depths, so you should still be able to use brackets to dereference. Just organize your code to use the right class (
Rgb32,Rgb565,Rgb24, etc).Regarding your compiler error, iostream doesn’t know how to convert a
Rgbto astring, so you naturally get this error. I would suggest creating aRgb::ToString()function.