Results 1 to 9 of 9

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

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default 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 :

    Qt Code:
    1. myclass::operator << (std::string data) {}
    2.  
    3. myclass::operator << (const char * data) {}
    4.  
    5. myclass::operator << (bool data) {}
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

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

    Ok , if I call class<<""; I see that function used is the bool ???????
    Using g++ 4.5.2:
    Qt Code:
    1. class MyClass{
    2. public:
    3. void operator << (std::string data) {
    4. cout << "string data";
    5. }
    6. void operator << (const char * data) {
    7. cout << "char data";
    8. }
    9. void operator << (bool data) {
    10. cout << "bool data";
    11. }
    12. };
    13.  
    14. MyClass m;
    15. m <<""; //!< displays "char data"
    16. m << NULL; //!< wont compile
    17. };
    To copy to clipboard, switch view to plain text mode 
    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 ).

  3. #3
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

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

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

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

  5. #5
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:
    Qt Code:
    1. my_debug<<"The a_data is:"<<a_data<<"the b_data is:"<<b_data<<"@";
    To copy to clipboard, switch view to plain text mode 
    but I think tha something like this is more clear:
    Qt Code:
    1. my_debug<<"The a_data is:"<<a_data<<"the b_data is:"<<b_data<<NULL;
    To copy to clipboard, switch view to plain text mode 

    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)
    Qt Code:
    1. mydebug & mydebug::operator << (std::string data) {
    2. os<<data;
    3. return *this;
    4. }
    To copy to clipboard, switch view to plain text mode 


    Thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: I'm unable to define a function able to receive NULL or "" if I want

    This code is dangerous:
    Qt Code:
    1. void operator << (const char * data) {
    2. cout << "char data";
    3. }
    To copy to clipboard, switch view to plain text mode 
    since its not known where the data ends.
    you need to supply size as well:
    Qt Code:
    1. void operator << (const char * data,unsigned long lSize) {
    2. std::string _string(data,lSize);
    3. cout << _string;
    4. }
    To copy to clipboard, switch view to plain text mode 
    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:

    Qt Code:
    1. #define NULL (void *)0L;
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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:
    Qt Code:
    1. my_debug << end;
    To copy to clipboard, switch view to plain text mode 

    Have a look at how QTextStream does it with endl.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: I'm unable to define a function able to receive NULL or "" if I want

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

Similar Threads

  1. Replies: 2
    Last Post: 17th October 2010, 17:20
  2. Replies: 0
    Last Post: 4th October 2010, 21:53
  3. Replies: 9
    Last Post: 20th May 2010, 09:55
  4. Can I define "anonnymous" slots somehow?
    By agnus in forum Newbie
    Replies: 1
    Last Post: 18th November 2009, 22:36
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.