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 432205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:03:04+00:00 2026-05-12T20:03:04+00:00

I want to create a substr method in C++ in a string class that

  • 0

I want to create a substr method in C++ in a string class that I made.

The string class is based on C-style string of course, and I take care of the memory management.

I want to write a substr(start, length) function that can work on the regular way:

CustomString mystring = "Hello";

cout << mystring.substr(0,2); // will print "He"

And also in this way:

mystring.substr(1,3) = "DD"; // mystring will be "HDDo"

Notice that even though I get a 3 chars long sub-string, I put in the assignment only 2 chars and the output string will be HDDo, still.

Any idea how to get this done?

Thanks!

  • 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-05-12T20:03:04+00:00Added an answer on May 12, 2026 at 8:03 pm

    To support that, you’ll probably have to write your substr() to return a proxy object that keeps track of what part of the original string is being referred to. The proxy object will overload operator=, and in it will replace the referred-to substring with the newly assigned one.

    Edit in response to comments: the idea of a proxy is that it’s similar enough to the class for which it’s a proxy that returning a proxy is still a closed operation — i.e. from the user’s viewpoint, all that’s visible is the original type of object, but it has capabilities that wouldn’t be possible (or would be much more difficult to implement) without the proxy. In this case, we the proxy class would be private to the string class, so the user could never create an instance of the proxy class except as a temporary. That temporary can be used to modify its parent string if you assign to it. Using the proxy in any other way just yields a string.

    As to what this buys you over attempting to do it all inside the original string: each proxy object is a temporary object — the compiler can/will/does keep track of how to create temporaries as needed, destroys them properly at the end of a full expression, etc. The compiler also keeps track of what substring a particular assignment refers to, automatically converts one to a string when we try to use its value, and so on. Simply put, the compiler handles nearly all the hard work involved.

    Here’s some working code. The surrounding string class is pretty minimal (e.g. it has no searching capability). I’d expect to add a fair amount to a useful version of the string class. The proxy class, however, is complete — I wouldn’t expect to see it change much (if at all) in a feature-complete version of the string class.

    #include <vector>
    #include <algorithm>
    #include <iostream>
    #include <iterator>
    
    class string { 
        std::vector<char> data;
    public:
        string(char const *init) { 
            data.clear();
            data.assign(init, init+strlen(init));
        }
    
        string(string const &s, size_t pos, size_t len) {
            data.assign(s.data.begin()+pos, s.data.begin()+pos+len);
        }
    
        friend class proxy;
    
        class proxy {
            string &parent;
            size_t pos;
            size_t length;
        public:
            proxy(string &s, size_t start, size_t len) : parent(s), pos(start), length(len) {}
    
            operator string() { return string(parent, pos, length); }
    
            proxy &operator=(string const &val) { 
                parent.data.erase(parent.data.begin()+pos, parent.data.begin()+pos+length);
                parent.data.insert(parent.data.begin()+pos, val.data.begin(), val.data.end());
                return *this;
            }
        };
    
        proxy substr(size_t start, size_t len) { 
            return proxy(*this, start, len);
        }
    
        friend std::ostream &operator<<(std::ostream &os, string const &s) { 
            std::copy(s.data.begin(), s.data.end(), std::ostream_iterator<char>(os));
            return os;
        }
    };
    
    #ifdef TEST
    
    int main() { 
        string x("Hello");
    
        std::cout << x << std::endl;
    
        std::cout << x.substr(2, 3) << std::endl;
    
        x.substr(2, 3) = "DD";
    
        std::cout << x << std::endl;
    
        return 0;
    }
    
    #endif
    

    Edit 2:
    As far as substrings of substrings go, it depends. The one situation that’s not currently covered is if you want to assign to a substring of a substring, and have it affect the original string. If you want something like x=y.substr(1,4).substr(1,2); it’ll work fine as-is. The first proxy will be converted to a string, and the second substr will be invoked on that string.

    If you want: x.substr(1,4).substr(1,2) = "whatever"; it won’t currently work. I’m not sure it accomplishes much, but on the assumption that it does, the addition to support it is fairly minimal — you’d add a substr member to proxy:

    proxy substr(size_t start, size_t len) { 
        return proxy(parent, pos+start, len);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want create wordpress website into which I want create user management... That means
I want to create a function that returns a substring of a specific string
i want create a custom json data from the mssql 2008 results so that
I want to create a new field (or two) in my table that is
I want to create a php script that will ping a domain and list
i want create multiple search where statement $where_search is a multiple condition from post
I want create an application with animate button? how can i do? after click
i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =
I want create a excel with Apache POI in java and I must insert
i want create image animation , i have 50 images with png format now

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.