Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9253415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:12:46+00:00 2026-06-18T11:12:46+00:00

EDIT : The code that immediately follows, is the working version, located within the

  • 0

EDIT: The code that immediately follows, is the working version, located within the header

inline char * operator & (const char String1 [], const MyStringClass & String2)
{
    int length = strlen (String1) + String2.Length();
    char * pTemp = new char [length + 1];
    strcpy (pTemp, String1);
    strcat (pTemp, String2.GetStr());   
    return pTemp;
}

This is the first time I’ve felt the need to ask a question, since I’ve been unable to find helpful information on my own (via search, Google, book, etc). My course book is C++ Primer 5th Edition and I’ve read Ch. 14 which covers operator overloading. I’m not necessarily looking for an “answer” but rather a nudge in the right direction (because I do want to learn this stuff).

The assignment has us creating our own string class and overloading a bunch of operators that will take a class object on either side – with exception to the assignment operator which may only take a class object on the left side. I’ve played around with all sorts of return types (this cannot be a member function; efforts to make this a friend function failed).

/* 
   Note: return by value, otherwise I get a warning of returning the address
   of a local variable, temporary. But no matter the return type or what I'm
   returning, I always get the error: C2677: binary '&' : no global operator 
   found which takes type 'MyStringClass' (or there is no acceptable 
   conversion)
*/

MyStringClass operator & (const char String1 [], const MyStringClass & String2)
{
    /*
       The only requirement is that the left side has const char [] so that
       (const char []) & (MyStringClass &) will concatenate. There is no return 
       type requirement; so, I could either try and return a string object or
       an anonymous C-type string.

       cout << StringOject1 << endl; // this works
       cout << (StringObject1 & "bacon") << endl; // so does this; 
       // another function overloads & such that: obj & const char [] works

       cout << ("bacon" & StringObject1) << endl; // but not this
    */

    MyStringClass S (String1); // initialize a new object with String1
    S.Concat (String2); // public member function Concat() concatenates String2
                        // onto String1 in S
    return S; // this does not work

    /* a different way of trying this... */
    int Characters = strlen (String1) + String2.Length();
    int Slots = Characters;
    char * pTemp = new char [Slots + 1];
    strcpy (pTemp, String1);
    strcat (pTemp, String2.pString); // this won't work; pString is a private 
                                     // member holding char * and inaccessible
    // making it pointless to try and initialize and return an object with pTemp
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:12:47+00:00Added an answer on June 18, 2026 at 11:12 am

    Have looked at your code and from what I can understand, you’re probably looking for something like this:

    class MyStringClass
    {
    public:
        const char* data() const;
    
    private:
    const char* charptr;
    };
    
    
    const char* MyStringClass::data() const
    {
        return charptr;
    }
    
    
    MyStringClass operator & (const char String1 [], const MyStringClass & String2)
    {
        /* a different way of trying this... */
        int len = strlen(String1) + String2.Length();
        char * pTemp = new char [len + 1]; //total length of both strings
        strcpy (pTemp, String1);
        strcat (pTemp, String2.data()); // you need to have a public member function that returns the string as const char*
        MyStringClass str(pTemp); //requires MyStringClass to have constructor that takes char*
        return str; //return the string
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this html code that i want to edit with jQuery. Here is
I am writing some code that will go through a file, edit it as
EDIT: I realized that this code compiles and works: #include <iostream> template<class Something> class
Edit: original question below, but I revise it now that I have some code
Edit my code to make it work please. Edited for latest version. Here is
I have a line of php code that looks like this: echo <script>$('#edit_errors').html('<h3><em>Please Correct
EDIT : the code has been trimmed down to contain only what is necessary
EDIT - The code looks strange here, so I suggest viewing the files directly
EDIT: This code now works correctly, I only left it in case someone finds
Edit: Added code (Exception on line 095, 5th time it's hit.) public DataTable ParseBarcodes(String[]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.