| 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
.
|
|
|
|

|
Related Articles |
|
|