Array operator overloading from typedef?
Sorry if this is a fairly simple thing... I'm an ol' C timer, not much
of one for C++. But, I do have an interesting problem here, and not
sure if there's a solution to it [the way I'd like]. Perhaps someone
here can show me how - or perhaps present me with other options on
getting the behavior I'm looking for.
Also, let me apologize in advance for the "cryptic" post. The codebase
is rather large that I'm working in, and instead of pasting lots of
code, I'll first try just a simplified explanation (and hopefully I'll
do a decent job).
I have a type that's - for all intensive purposes - a kind of variant.
We'll call this class Foo. There's nothing really special in it,
except that it ends with byte[0], and we always allocate more space
for the data that Foo manages.
struct Foo
{
// stuff
char byte[0];
};
Now, we always have a pointer to a Foo - never a reference or a static
allocation. And because Foo is actually hidden a bit, there's a
different type that is exposed to all the other code... let's call it
Thing:
typedef Foo* Thing;
Now, what I'd _like_ to do is overload the [] operator in such a way
that, given a Thing, I can index into Foo::byte and get what I need. I
can obviously put the [] operator inside of Foo, but then I end up
with a lot of code that looks like this:
Thing p = ... ;
doSomething((*p)[4]);
Which isn't exactly very readable. I've also toyed with making Thing
something other than a typedef and putting the [] operator inside of
that. In my side test app, that works, but there would be a lot of
code to change if I decided to go down that road; I'd rather not.
If all else fails, I'm just gonna stick a stupid accessor method in
there (get(i) or similar) and be done with it. But, I'm hoping there's
some college student out there who knows all the wonderful C++
syntactic tricks who can show me a solution. :)
Any ideas?
Thanks!
Jeff M.