J
Jacek Dziedzic
Hi!
Let's say I have a class called Triplet that serves as an envelope
for double[3], ie.
class Triplet {
public:
Triplet() {/*...*/}
/*
some things that double[3] doesn't have, like
a << operator to send it to a stream
*/
private:
double storage[3];
};
I'm having a problem with the subscript operator, I tried
double& Triplet:perator[](const unsigned int i) const {
return storage;
}
and was quite surprised to see it won't compile. It works
fine, however, when I change
"double storage[3]" to "double *storage" and allocate it
accordingly.
I guess I'm being bitten by the differences between array
of double and pointer to double when it comes to references,
but can someone shed some light on why what I attempted is
not possible? I got away with something like
return (static_cast<double*>(&(storage[0])));
while also making the operator[] non-const, but it sure looks ugly.
So... what is it with the array that doesn't allow what
I'm trying to do? Is there a cleaner way (changing the array
to a pointer and doing new[] will be overkill, I use these
Triplets in huge matrices up to 8100x8100).
Let's say I have a class called Triplet that serves as an envelope
for double[3], ie.
class Triplet {
public:
Triplet() {/*...*/}
/*
some things that double[3] doesn't have, like
a << operator to send it to a stream
*/
private:
double storage[3];
};
I'm having a problem with the subscript operator, I tried
double& Triplet:perator[](const unsigned int i) const {
return storage;
}
and was quite surprised to see it won't compile. It works
fine, however, when I change
"double storage[3]" to "double *storage" and allocate it
accordingly.
I guess I'm being bitten by the differences between array
of double and pointer to double when it comes to references,
but can someone shed some light on why what I attempted is
not possible? I got away with something like
return (static_cast<double*>(&(storage[0])));
while also making the operator[] non-const, but it sure looks ugly.
So... what is it with the array that doesn't allow what
I'm trying to do? Is there a cleaner way (changing the array
to a pointer and doing new[] will be overkill, I use these
Triplets in huge matrices up to 8100x8100).