|
From: Dan P. <ba...@al...> - 2006-04-28 04:06:25
|
It's really not a quirk but sort of "behaves as designed". A pointer
is a totally distinctive type from an object instance in C++. If you
dereference it, it becomes an object reference. So yeah, you can't
use operator<< on a pointer. :\ You might be able to do something
like this though:
inline operator<<(ConsoleText *, foo) {
}
i.e. not part of a class. I'm not sure if that'll work but it might
let you.
This example you gave is probably one of the reasons they added the
'references' in C++ instead of just keeping pointers. That way you
can basically pass pointers but you can treat them like objects. I
just wish you could legally have null references and reassign them.
On Apr 27, 2006, at 8:53 PM, Sam Steele wrote:
> I ran into another C++ quirk while working on TikiSnake. If you
> look at the code, you'll see that I have to have my Drawable be an
> actual object, not a pointer, otherwise C++ complains about my
> overloaded << operator. The following works fine:
>
> ConsoleText ct(blah);
>
> ct << "Hello world!";
>
> But the following doesn't:
>
> ConsoleText *ct = new ConsoleText(blah);
>
> ct << "Hello world!";
>
> /Users/sam/Projects/Console/src/snake.cpp:73: error: invalid
> operands of types 'Tiki::GL::ConsoleText*' and 'const char [13]' to
> binary 'operator<<'
>
> You can do:
>
> *ct << "Hello world";
>
> But the syntax looks a little quirky.
>
> Is there a way to tweak my declaration of the << operator to allow
> it to work through a pointer?
>
> -Sam
|