rasbury wrote:[color=blue]
> If I were to include in the definition of a string class the[/color]
following[color=blue]
> operators:
>
> class String {
> public:
> // ...
> operator const char *() const; // conversion to C-style[/color]
string[color=blue]
> char operator[](size_t i) const; // character indexing
> // ...
> };
>
> I can't use the indexing operator because the compiler doesn't know[/color]
whether[color=blue]
> I mean to index the String object directly or to convert to a const[/color]
char *[color=blue]
> and then index on that. It is clear to me, at least in this case,[/color]
that[color=blue]
> having gone to the trouble of defining an indexing operator for the[/color]
class,[color=blue]
> it is that function I want to call. I would have thought that this[/color]
would be[color=blue]
> obvious to any compiler too - only attempt to perform conversions if[/color]
an[color=blue]
> expression doesn't make sense without them.
>
> Could anyone explain to me why C++ doesn't behave like this? I can't[/color]
think[color=blue]
> of any example where this would be undesirable (although I could[/color]
believe[color=blue]
> they might exist), and it seems that it would be very useful,[/color]
particularly[color=blue]
> when interfacing with C code which expects more primitive types.
>
> Richard[/color]
I tried your code with the Comeau compiler
(
http://www.comeaucomputing.com/tryitout/). It compiles fine. You are
probably using a buggy compiler.
Hope this helps,
-shez-