Thread: Overloading

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question Overloading

    Hi!

    I have a class named String which contains member "char *text" and method "void print() const;".

    Whenever i declare String class and i want to print that string on the screen i always get the address of the class String (example : 2DEE22).

    I just want to do this:

    Code:
      ...
      String s("HELLO !!");
      cout << s;
    So this must print "HELLO !!" on the screen and not "2DEE22". What I need is that cout << will automatically call print() method.

    What do I have to do to correct this problem?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    How is String defined?
    Why are you using a non standard string class when the standard library provides one for you to use?
    Did you write a
    Code:
    friend std::ostream& operator<<(std::ostream &, const String &)
    function?
    Do you like pie?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How is String defined?
    Like string except with an upper case S.

    >Why are you using a non standard string class when the standard library provides one for you to use?
    For fun.

    >Did you write a <snip> function?
    I would wager not. But for grins, how about defining a
    Code:
    operator const char *() { return text; }
    member function?

    >Do you like pie?
    I like pie.
    My best code is written with the delete key.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Unfortuantly I think most of the people asking questions like the OP did aren't doing it for fun

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I think most of the people asking questions like the OP did aren't doing it for fun
    What a shame, it's quite an experience. Especially if you do it right and try to meet the standard's requirements.

    That reminds me, maybe I should update my prestring class to be more l33t. It's been a while since I wrote it.
    My best code is written with the delete key.

  6. #6
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    I already have this method "operator const char *() { return text; }" and it is not working. I still get the address instead of the actual text.

    Any other suggestions?


    I like pie too .
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >and it is not working
    You aren't doing something right then. But that's a moot issue anyway because using an implicit conversion overload is fraught with danger (and everyone knows that you can't argue with a statement that uses the word fraught).

    >Any other suggestions?
    Overload the << operator as Thantos suggested.
    My best code is written with the delete key.

  8. #8
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    How am I supposed to define that friend method in .cpp file?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How am I supposed to define that friend method in .cpp file?
    Your best bet would be to generalize print() for all output streams, then you can do this:
    Code:
    ostream& String::operator<< ( ostream& out, const String& s )
    {
      s.print ( out );
      return out;
    }
    My best code is written with the delete key.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Psst Prelude the function should be:
    Code:
    ostream& operator<< ( ostream& out, const String& s )
    {
      s.print ( out );
      return out;
    }
    since its a friend function and not a member function.

    And your right there is no way to argue with fraught

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Psst Prelude the function should be
    Shh! If you don't say anything then maybe nobody will notice.

    >And your right there is no way to argue with fraught
    My code is fraught with perfection. If it looks wrong then that's only because, in my awesomeness, I anticipated a future addition to the language and started using it. Yea, that's it. I'm so awesome that I can predict the tides of ISO! Hahaha! Make way for my head!
    My best code is written with the delete key.

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    This just in: Prelude's head has been entered into the Macy's Thanksgiving Day Parade. She is expected to win best ballon.

  13. #13
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    My prof is all against friend functions, He claims they break encapsulation and make code hard to debug.

    He suggests using accessor functions to manipulate the class.

    I am refering to overloading << and >> mainly.

    He made a good argument and it has made me rethink my method of overloading the ostream operators.

  14. #14
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by curlious
    My prof is all against friend functions.
    Probably he says also that void main is ok....
    As far as I know, friend functions are defined by who creates the class, therefore the encapsulation argument, for me, is non-sense.

    GaPe, those strange numbers are obviously memory adresses. You need some where to de-reference a pointer.
    Last edited by xErath; 11-15-2004 at 09:31 PM.

  15. #15
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    There is good logic behind that. In general, I tend to avoid friends. If it does need access to the internals of the class, it probably should be a member.

    Here is a well articulated rant (particularly against std::string): http://www.gotw.ca/gotw/084.htm
    Last edited by Zach L.; 11-15-2004 at 09:41 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  3. overloading operator problems
    By almich in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2004, 04:10 PM
  4. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 04:06 PM
  5. overloading
    By theLukerBoy in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2002, 08:49 PM