Results 1 to 4 of 4

Thread: operator<<

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default operator<<

    Hello,
    could anyone at first sight tell me about why this below doens't work with keyword "const" (that I guess the right way) and it works without it? thanks
    Qt Code:
    1. friend std::ostream& operator<<(std::ostream& os, const Apple& a) { // <- "const"
    2. return os << "kind = " << a.getKind().c_str() << "\t"
    3. << "weight = " << a.getWeight() << "\t" << "status = " << a.getStatus().c_str() << endl;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 10th June 2008 at 00:24. Reason: wrapped too long line
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: operator<<

    Quote Originally Posted by mickey View Post
    why this below doens't work with keyword "const" (that I guess the right way) and it works without it?
    Most likely because at least one of the Apple::getXxx() methods is not const.

    http://www.parashift.com/c++-faq-lit...rrectness.html

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: operator<<

    It was that! Furthermore always at first sight: I have to do this:
    Qt Code:
    1. class Fruit {
    2. virtual friend ostream& operator<<(ostream& os, const Fruit& f) { }
    3. };
    4. class Apple : public Fruit {
    5. friend ostream& operator<<(ostream& os, const Apple& a) { }
    6. };
    7. //main.cpp
    8. Fruit* f = new Apple;
    9. cout << *f;
    To copy to clipboard, switch view to plain text mode 
    This above doens't work.......what I like obtain is make virtual the operato<< (I guess you can guess it from the piece of code).....

    thanks....
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: operator<<

    You can't define ostream & ostream::operator<< ( const Fruit & a ) in Fruit class, because it belongs to ostream class, and you can't change the definition of ostream class, so the only place you can put that operator<< is outside any class:
    Qt Code:
    1. class Fruit
    2. {
    3. ...
    4. virtual void storeOrWhatever( ostream & os ) const = 0;
    5. };
    6.  
    7. class Apple : public Fruit
    8. {
    9. ...
    10. void storeOrWhatever( ostream & os ) const { ... }
    11. };
    12.  
    13. ostream& operator<<( ostream& os, const Fruit & a )
    14. {
    15. a.storeOrWhatever( os );
    16. return os;
    17. }
    To copy to clipboard, switch view to plain text mode 
    This way you can easily call the base class implementation and you don't need any friends.

Similar Threads

  1. operator<< and templates
    By mickey in forum General Programming
    Replies: 3
    Last Post: 30th May 2008, 17:13
  2. operator<< QString
    By kiker99 in forum Qt Programming
    Replies: 6
    Last Post: 5th May 2006, 00:23

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
  •  
Qt is a trademark of The Qt Company.