473,386 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
}

Oct 12 '05 #1
7 2013

pn*******@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.

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

Oct 12 '05 #2
"pn*******@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
kb******@gascad.at
Oct 12 '05 #3
pn*******@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.
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
Oct 12 '05 #4
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...

Oct 12 '05 #5
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...

Oct 12 '05 #6
pn*******@gmail.com 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
Oct 12 '05 #7
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!

Oct 12 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Simon Ford | last post by:
Hi All, I'm having trouble understanding exactly how I can do some specific implicit casting. There are two problems here; does anyone know what I should be doing? //---------- // (1)...
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
1
by: Kel Good | last post by:
Hello, I recently created a structure in C# that offers several implicit conversion, hoping I could then reference the C# project from a VB.Net project, and use this object in VB.Net. It does...
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
3
by: Jon Shemitz | last post by:
Why would you ever want to define both "public static bool operator true" and "public static bool operator false" when you can just define "public static implicit operator bool"? Is there syntax...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
10
by: Pieter Breed | last post by:
Hi All, Please excuse me, but the bulk of my post will be a code post. It describes some weirdness with regards to the implicit casting operator. The crux of the problem is this: I want to...
1
by: not_a_commie | last post by:
I have an Angle class that I store angles in. It's basically just a bunch of fancy functions for manipulating a double. It has implicit casts for converting to/from double. Due to the project...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.