need help with a method

This is a discussion on need help with a method within the C++ Programming forums, part of the General Programming Boards category; This code handles text + const char* : Code: text& text::operator+ (const char* c) { //code } but how can ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    need help with a method

    This code handles text + const char* :
    Code:
    text& text::operator+ (const char* c)
    {
       //code
    }
    but how can I handle a const char* + text expression ?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789

    Re: need help with a method

    Originally posted by laasunde
    This code handles text + const char* :
    Code:
    text& text::operator+ (const char* c)
    {
       //code
    }
    but how can I handle a const char* + text expression ?
    You need to overload the operator for + for that situation...you cant really do it for char* as you cant overload operators on a built in data type

    Code:
    text operator+ (const char* c,const text& t)
    {
       //code
    }

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    operator + must return an object an not a reference.

    For a detailed explanation consult the bible part 1 (effective C++ by Scott Meyers) item 23 (Dont try to return a reference when you must return an object).
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    operator + must return an object an not a reference.
    Correct me if I'm wrong...
    I think so because if you creat the object in the function it should be destroyed aftert the function is executed.

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by ammar
    Correct me if I'm wrong...
    I think so because if you creat the object in the function it should be destroyed aftert the function is executed.
    Yes....if you return a reference, the object you are providing the reference to will be out of scope, and therefore effectively out of existance by the time you use it.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by Fordy
    Yes....if you return a reference, the object you are providing the reference to will be out of scope, and therefore effectively out of existance by the time you use it.
    Are you saying this wont work ?

    Code:
    text& text::operator+ (const char* c)
    {
      text *obj = new text()
    
      //code  
    
      return obj;
    }

  7. #7
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Originally posted by laasunde
    Are you saying this wont work ?

    Code:
    text& text::operator+ (const char* c)
    {
      text *obj = new text()
    
      //code  
    
      return obj;
    }
    That will work because the memory is dynamically allocated, but this is what you musn't do :
    Code:
    text& text::operator+ (const char* c)
    {
      text obj;
    
      //code  
    
      return obj;
    }

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by laasunde
    Are you saying this wont work ?

    Code:
    text& text::operator+ (const char* c)
    {
      text *obj = new text()
    
      //code  
    
      return obj;
    }
    Firstly, no it wont as you are trying to return a pointer, when the func user will expect a reference....

    Secondly, its an stupid way of doing things as the user will have to call delete to free that memory for a simply operator+ addition, and even more worryingly they wont have to the option to even do this as they wont be able to even access the pointer to actually call delete on it!

  9. #9
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Originally posted by Fordy
    Firstly, no it wont as you are trying to return a pointer, when the func user will expect a reference....
    I didn't even see that, it should've been :
    Code:
    text& text::operator+ (const char* c)
    {
      text *obj = new text()
    
      //code  
    
      return *obj;
    }

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by The Dog
    I didn't even see that, it should've been :
    Code:
    text& text::operator+ (const char* c)
    {
      text *obj = new text()
    
      //code  
    
      return *obj;
    }
    Sorry I forgot about the dereference operator.

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by Fordy
    Firstly, no it wont as you are trying to return a pointer, when the func user will expect a reference....

    Secondly, its an stupid way of doing things as the user will have to call delete to free that memory for a simply operator+ addition, and even more worryingly they wont have to the option to even do this as they wont be able to even access the pointer to actually call delete on it!
    Your first point was a typo, the dereference operator should have been there.

    Your second point, I see your point but how would solve the issue ?

    Appreciate any response.

    Cheers

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by laasunde
    Your first point was a typo, the dereference operator should have been there.
    It doesnt matter...you are still creating an opject on the heap that you have no real way of freeing with a call to delete..therefore you can expect some nasty memory leaks

    Originally posted by laasunde
    Your second point, I see your point but how would solve the issue ?
    Always return an object by value in this situation....its not the most efficient way, but its the safe way

  13. #13
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by Fordy
    It doesnt matter...you are still creating an opject on the heap that you have no real way of freeing with a call to delete..therefore you can expect some nasty memory leaks

    Always return an object by value in this situation....its not the most efficient way, but its the safe way
    So my overloading method should look something like this then :
    Code:
    text text::operator+ (const char* c)
    {
      text obj;
    
      //  
    
      return obj;
    }

  14. #14
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by Fordy
    It doesnt matter...you are still creating an opject on the heap that you have no real way of freeing with a call to delete..therefore you can expect some nasty memory leaks
    Any good utillitys that can help me find possible memory leaks ?

  15. #15
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    [QUOTE Stoned_Coder ]
    operator + must return an object an not a reference.

    For a detailed explanation consult the bible part 1 (effective C++ by Scott Meyers) item 23 (Dont try to return a reference when you must return an object). [/QUOTE]

    Why can't you just return a reference if that's effecient?
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 06:50 PM
  2. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 04:17 PM
  3. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-07-2008, 11:30 PM
  4. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 07:51 AM
  5. Replies: 2
    Last Post: 01-22-2008, 03:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21