I'm unable to define a function able to receive NULL or "" if I want
I want to pass a "" or NULL value to a function which has the operator << overloaded.
I have :
Code:
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
Re: I'm unable to define a function able to receive NULL or "" if I want
NULL is basically expanded to 0, or sometimes to (void*)0 (as its meant to be used as pointer).
Quote:
Ok , if I call class<<""; I see that function used is the bool ???????
Using g++ 4.5.2:
Code:
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 ).
Re: I'm unable to define a function able to receive NULL or "" if I want
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....
Re: I'm unable to define a function able to receive NULL or "" if I want
Yeah, good question, how to distinguish between three zeros:) Honestly, I don't know.
Better tell us what are you trying to achieve.
Re: I'm unable to define a function able to receive NULL or "" if I want
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:
Code:
my_debug<<"The a_data is:"<<a_data<<"the b_data is:"<<b_data<<"@";
but I think tha something like this is more clear:
Code:
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 :
[CODE] 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
Re: I'm unable to define a function able to receive NULL or "" if I want
This code is dangerous:
Code:
void operator << (const char * data) {
cout << "char data";
}
since its not known where the data ends.
you need to supply size as well:
Code:
void operator << (const char * data,unsigned long lSize) {
std::string _string(data,lSize);
cout << _string;
}
Quote:
I want to pass a value to my function with the idea of to mean 'end'.
Quote:
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.
Re: I'm unable to define a function able to receive NULL or "" if I want
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:
Code:
#define NULL (void *)0L;
Re: I'm unable to define a function able to receive NULL or "" if I want
Usually you define a set of flags and have an overload accepting those flags. Then you can do something like:
Have a look at how QTextStream does it with endl.
Re: I'm unable to define a function able to receive NULL or "" if I want
@high_flyer:
Quote:
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.