Thread: Operater overloading - (const char + CString)

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

    Red face Operater overloading - (const char + CString)

    I'm creating a String class without any class libraries included.
    To overload operator methods like CString + CString, CString + const char and CString + const char * aren't a problem at all.
    But when I'm supposed to switch the factors I'm about to deal with my dearest problem. Suppose I'm going to add const char to CString.

    If I'm about to create the method heading like the others it would look something like this:
    const char operator+(CString &bla);
    And of course, that won't work.

    I've searched the entire Internet, I believe, and cannot find huge amount of examples of my problem. So I get kind of nervous about this.

    I found a way though, it's possible to write:
    CString& operator+(const char &c, CString &s);
    When I compile, I get one error:
    error C2804: binary 'operator +' has too many parameters

    So, any suggestions how to get the syntax right?

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    try including this line in the class as a public member function (in the header file if class is modularized):

    friend CString & operator + (const char *, CString &);

    then outside class declaration (in cpp file if class is modularized)
    you would use this:
    Code:
    CString & operator + (const char * lhs, CString & rhs)
    {
        CString LHS (lhs);//use constructor to convert lhs into a CString;
        CString result = LHS + rhs;
        return result;
    }
    then in main() or wherever

    const char * lhs = "Hello ";
    CString rhs = " World!";
    CString combined;
    combined = lhs + rhs;


    Look up using friend functions in your favorite tutorial/text for details.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    returning a reference to a local object is not allowed and that is why operator + NEVER returns a reference but instead MUST return an object.
    Keep trying!
    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
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    Smile

    Thanks Elad, it worked!

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    No it didnt. Not if you are still returning a reference. The object you are referring to is gone and destroyed.
    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

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    Yes, It did, with modifications.

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Stoned_Coder is right.....

    If you are doing something like

    str = str1 + str2 + str3;

    you might find a problem

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    Cstring sum = cChar (const char) + str1 (CString object).

    In the corresponding method i copy the parameters rhs (str1) and lhs (cChar) into a new CString object, where I modify it and checks its final length. Then I copy it to class_String, which is declared private, and in my case is sum.class_String. My print() function prints the final class_String for sum.

    It all works fine with no problems or error messages. Maybe it's a bad method, but as long as it works..

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    As long as you are returning an object and not a reference all will be well. You cannot return a reference from operator +. Look at your code. See for yourself that you are returning a reference to a destroyed object. This is highly illegal and just plain stupid!
    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

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    Yes, I return an object, NOT a reference. Satisfied ?

  11. #11
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by auth
    Yes, I return an object, NOT a reference. Satisfied ?
    Code:
    CString & operator + (const char * lhs, CString & rhs)
    You sure there chief?

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    Yes Sir, due to the modification the "&" is gone.

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Maybe you should listen to what these guys are saying? They speak the truth...


    Code:
    char * bad_idea() {
    
     char uncertainty[] = "This object will die at some point after"
                     " this function returns. This means that although"
                     " this data may not dissappear from memory"
                     " immediately, there is no guarantee that"
                     " it will be there in the future. Hope for the best, eh?"; 
    
     return uncertainty;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Sebastiani
    Maybe you should listen to what these guys are saying? They speak the truth...
    He said he returned an object, not a reference nor a pointer to a local object .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I'm a slow typist.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Display Lists (OpenGL)
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 06-05-2005, 12:11 PM
  5. AnsiString versus char *
    By Zahl in forum C++ Programming
    Replies: 35
    Last Post: 10-16-2002, 08:38 PM