PDA

View Full Version : I'm unable to define a function able to receive NULL or "" if I want



tonnot
24th February 2011, 09:02
I want to pass a "" or NULL value to a function which has the operator << overloaded.
I have :


myclass::operator << (std::string data) {}

myclass::operator << (const char * data) {}

myclass::operator << (bool data) {}

Ok , if I call class<<""; I see that function used is the bool ???????

How can I do to detect "" or NULL ???? What kind of types are ?
Any help ? Thanks

stampede
24th February 2011, 10:01
NULL is basically expanded to 0, or sometimes to (void*)0 (as its meant to be used as pointer).


Ok , if I call class<<""; I see that function used is the bool ???????
Using g++ 4.5.2:

class MyClass{
public:
void operator << (std::string data) {
cout << "string data";
}
void operator << (const char * data) {
cout << "char data";
}
void operator << (bool data) {
cout << "bool data";
}
};

MyClass m;
m <<""; //!< displays "char data"
m << NULL; //!< wont compile
};
m << NULL; wont compile, because compiler is confused - NULL could be interpreted as a char *, but on the other side it can be converted to bool as well - its the same as m << 0;.
If you add operator << (int), it will be used when you call m<<NULL;, because there will be no ambiguation in this case ( because NULL is 0 ).

tonnot
24th February 2011, 10:56
Thanks stampede
But ... how I can to distinguish between
m << NULL;
or
m <<0;
or
m<<my_int; (supossing my_int =0)

Every of three has 0....

stampede
24th February 2011, 12:36
Yeah, good question, how to distinguish between three zeros:) Honestly, I don't know.
Better tell us what are you trying to achieve.

tonnot
24th February 2011, 13:04
I want to pass a value to my function with the idea of to mean 'end'.
Now I'm passing "@" but I dont like to use a char in particular.
My funcion recieves data using << operator, I save it to a sstream, and at the end I want to 'print' it to my window, to the qt debug area or to a log file.
I have:

my_debug<<"The a_data is:"<<a_data<<"the b_data is:"<<b_data<<"@";
but I think tha something like this is more clear:

my_debug<<"The a_data is:"<<a_data<<"the b_data is:"<<b_data<<NULL;

Because I dont know how to tell my function to do what I want with this line :

my_debug<<"The a_data is:"<<a_data<<"the b_data is:"<<b_data;

My << operators are similar to : (one for each type)
[CODE]mydebug & mydebug::operator << (std::string data) {
os<<data;
return *this;
}


Thanks

high_flyer
24th February 2011, 13:15
This code is dangerous:

void operator << (const char * data) {
cout << "char data";
}

since its not known where the data ends.
you need to supply size as well:

void operator << (const char * data,unsigned long lSize) {
std::string _string(data,lSize);
cout << _string;
}


I want to pass a value to my function with the idea of to mean 'end'.

but I think tha something like this is more clear:
It definitively is not.
0 can mean so many things, and it can be there even if you didn't mean it to be there.
Usually it is done by defining some sort of a token that mean "END" - such that it will be unlikly to be something else or to be randomly occurring.

squidge
24th February 2011, 13:15
So what you want to do is something similar to endl for ostream? If so, why not define it as such?

If you really want to use NULL, then define it:



#define NULL (void *)0L;

wysota
24th February 2011, 13:16
Usually you define a set of flags and have an overload accepting those flags. Then you can do something like:

my_debug << end;

Have a look at how QTextStream does it with endl.

stampede
24th February 2011, 13:22
@high_flyer:

This code is dangerous:
This particular code is quite safe, look that the data is not sent to cout at all ;) But you are right, in "final" code the length limit is needed, this one was just for testing.