B
Ben
Hi all,
I know what I need to do if data_ in the code below were a uchar*, but
what about when it's an uchar array? Do I need to specify my own
copy-constructor and assignment operator?
#include <iostream>
typedef unsigned char uchar;
class ArrayTest {
public:
uchar& operator[](size_t index) {
if (index >= 8) {
throw new std::range_error("Index out of bounds");
} else {
return data_[index];
}
}
private:
uchar data_[8];
};
int main(int argc, char* argv[])
{
ArrayTest a1;
a1[3] = 3;
ArrayTest a2(a1); // Problem?
std::cout << static_cast<const int>(a2[3]) << std::endl;
return 0;
}
It appears to do what I want in VC7, but I don't know whether it's UB or
not.
Cheers!
Ben
I know what I need to do if data_ in the code below were a uchar*, but
what about when it's an uchar array? Do I need to specify my own
copy-constructor and assignment operator?
#include <iostream>
typedef unsigned char uchar;
class ArrayTest {
public:
uchar& operator[](size_t index) {
if (index >= 8) {
throw new std::range_error("Index out of bounds");
} else {
return data_[index];
}
}
private:
uchar data_[8];
};
int main(int argc, char* argv[])
{
ArrayTest a1;
a1[3] = 3;
ArrayTest a2(a1); // Problem?
std::cout << static_cast<const int>(a2[3]) << std::endl;
return 0;
}
It appears to do what I want in VC7, but I don't know whether it's UB or
not.
Cheers!
Ben