Implicit cast to object that implements overloaded operator



 DEVELOP > c-Plus-Plus > Implicit cast to object that implements overloaded operator

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 12 Oct 2005 08:17:34 AM
Object: Implicit cast to object that implements overloaded operator
i want to use the << operator defined for ostream with an object that
itself knows nothing of the operator, but is castable to ostream&. it
seems that C++ doesn't do implicit casts before invoking a custom
operator.
i've tested with visual c++ 7 and the comeau online compilers, both
required an explicit cast. is there a way to hint the compiler at using
the cast operator implicitely, or is this not part of the language at
all? (i have a scenario where this would come in very handy, though i
am not sure if i would like c++ to behave like this anyways :)
#include <iostream>
using namespace std;
class test
{
public:
operator ostream& ()
{
return cout;
}
};
void test()
{
test t;
t << "test\n"; // implicit cast doesn't compile
((ostream&)t) << "test\n"; // explicit cast works
}
.

User: "Karl Heinz Buchegger"

Title: Re: Implicit cast to object that implements overloaded operator 12 Oct 2005 08:34:08 AM
"pnsteiner@gmail.com" wrote:


i want to use the << operator defined for ostream with an object that
itself knows nothing of the operator, but is castable to ostream&. it
seems that C++ doesn't do implicit casts before invoking a custom
operator.

An operator is nothing else then an ordinary function with some strange syntax.
So when your compiler sees.

test t;

t << "test\n"; // implicit cast doesn't compile

it transforms this into:
t.operator<<( "test\n" );
Then the compiler scans the class test, searching for a function operator<<.
Finding none, the compiler searches the freestanding functions for a standalone
.... operator<<( test&, const char* );
Finding none, the compiler gives up and emits an error message.
The problem with that is that the compiler doesn't try to cast an
object to something else, in order to be able to come up with a function
it can call. Either that object's class (or one of its base classes) has
that function or it has not.
--
Karl Heinz Buchegger
kbuchegg@gascad.at
.
User: ""

Title: Re: Implicit cast to object that implements overloaded operator 12 Oct 2005 08:38:53 AM
thanks for the prompt answer. it's clear to me now why implicit cast
doesn't make any sense here, thinking of operators being nothing more
than fancy method invokation syntax...
.
User: "Victor Bazarov"

Title: Re: Implicit cast to object that implements overloaded operator 12 Oct 2005 08:56:49 AM
wrote:

thanks for the prompt answer. it's clear to me now why implicit cast
doesn't make any sense here, thinking of operators being nothing more
than fancy method invokation syntax...

Actually, if operator<< with 'ostream&' as LHS and 'const char*' as RHS
*were* a non-member, the conversion would be considered. Take a look:
struct A {
void operator + (const char*);
};
void operator - (A&, const char*);
// operator+ is a member, operator- is not
struct B {
operator A& ();
};
int main() {
B b;
b + "abc"; // error here
b - "abc"; // _no_ error here
}
V
.
User: ""

Title: Re: Implicit cast to object that implements overloaded operator 12 Oct 2005 09:08:42 AM
i see, very interesting. actually my problem was about the usage of
non-member operator functions, i was just unknowingly using the member
operator << defined in ostream& in my test program, not recognizing the
difference between member and non-member operator function definitions.
thanks alot!
.




User: "Greg"

Title: Re: Implicit cast to object that implements overloaded operator 12 Oct 2005 08:27:43 AM
wrote:

i want to use the << operator defined for ostream with an object that
itself knows nothing of the operator, but is castable to ostream&. it
seems that C++ doesn't do implicit casts before invoking a custom
operator.

i've tested with visual c++ 7 and the comeau online compilers, both
required an explicit cast. is there a way to hint the compiler at using
the cast operator implicitely, or is this not part of the language at
all? (i have a scenario where this would come in very handy, though i
am not sure if i would like c++ to behave like this anyways :)

#include <iostream>
using namespace std;

class test
{
public:
operator ostream& ()
{
return cout;
}
};

void test()
{
test t;

t << "test\n"; // implicit cast doesn't compile
((ostream&)t) << "test\n"; // explicit cast works
}

Instead of having test implement an ostream conversion operator, why
not simply write a global << operator for an ostream (on the left) and
a test (on the right)?
Greg
.
User: ""

Title: Re: Implicit cast to object that implements overloaded operator 12 Oct 2005 08:43:02 AM
because the given code is just a sample for my problem. i have a class
with lots of global << operator definitions all over the place, and i
would like to be able to use those on another (unrelated) class without
redefining all those operators...
.


User: "Victor Bazarov"

Title: Re: Implicit cast to object that implements overloaded operator 12 Oct 2005 08:29:22 AM
wrote:

i want to use the << operator defined for ostream with an object that
itself knows nothing of the operator, but is castable to ostream&. it
seems that C++ doesn't do implicit casts before invoking a custom
operator.

Only if the operator is declared a member. The compiler is not required
to apply all possible conversions to try to find which conversion would
be necessary to _then_ resolve a member function call.

i've tested with visual c++ 7 and the comeau online compilers, both
required an explicit cast. is there a way to hint the compiler at using
the cast operator implicitely, or is this not part of the language at
all?

There is no way to "hint".

(i have a scenario where this would come in very handy, though i
am not sure if i would like c++ to behave like this anyways :)

#include <iostream>
using namespace std;

class test
{
public:
operator ostream& ()
{
return cout;
}
};

void test()
{
test t;

t << "test\n"; // implicit cast doesn't compile
((ostream&)t) << "test\n"; // explicit cast works
}

Use what works.
V
.


  Page 1 of 1

1

 


Related Articles
Template virtual member method is not allowed. How to bypass that?
The destruction of a sub instantiated obj that =?iso-8859-1?Q?I=92m?=pointing too.
Compilers that do implement C++ Exception Specifications
#include headers that include this header
Re: C/C++ compiler that will work with Atmel STK500
SQL oriented scripting language that can be executed from a C++ code
Workaround for static methods that need to be virtual
Class that can log what it's doing...sometimes
User-defined manipulators that accept arguments
How do I write a function that accepts a 'printf' type parameter?
editor that offers a search only in cursor scope
xlc - A template dependent name that is a type must be qualified with "typename"
What is the data structure & algorithm that is fastest searching & sorting.
C++ objects that act like Java/C# objects.
Float-to-string conversion that raises exception instead of returning0.0 like atof
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER