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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:40:06+00:00 2026-05-29T23:40:06+00:00

For training, I’m trying to write a class like std::string. I have read that

  • 0

For training, I’m trying to write a class like std::string.
I have read that the method at(int) returns the reference to a character and throws an exception in the case the index is wrong (too long, or negative).
The operator[] instead doesn’t throw any exception.
But since it returns a reference, if the index is too long, what does it return?
This is the class I have:

class string
{
    public:
    static const unsigned int length_max=100;
    string(const char* field=NULL)
    {
        if(field!=NULL)
        {
            const unsigned int length=strlen(field);
            this->field=new char[length+1];
            this->length=length;
            for(unsigned int i=0;i<=length;i++)
                this->field[i]=field[i];
        }
        else
        {
            this->field=NULL;
            length=0;
        }
    }
    string& operator=(const char* field)
    {
        const unsigned int length=strlen(field);
        if(this->field!=NULL)
            delete this->field;
        this->field=new char[length];
        this->length=length;
        for(unsigned int i=0;i<length;i++)
            this->field[i]=field[i];
        return *this;
    }
    string& operator= (const string& str)
    {
        *this=str.field;
        return *this;
    }
    operator char* ()
    {
        return field;
    }
    friend std::ostream& operator<< (std::ostream& out, string& str);
    friend std::istream& operator>> (std::istream& in, string& str);
    private:
    char* field;
    unsigned int length;
};

std::ostream& operator<<(std::ostream& out, string& str)
{
    out << str.field;
    return out;
}

std::istream& operator>> (std::istream& in, string& str)
{
    char temp[string::length_max];
    in >> temp;
    str=temp;
    return in;
}

I want to make the operator[], I also could make it throw an exception, but the problem is that I don’t know what shall it return in the case the index is too long.
For example the operator[] overloading is this:

    char& operator[] (int i)
    {
        try
        {
            if(i<0 || i>=length)
                throw indexException();
            return field[i];
        }
        catch(indexException& e)
        {
            std::cerr << e.what() << std::endl;
        }
    }

This is the exception:

class indexException:std::exception
{
    public:
    virtual const char* what()
    {
        return "Index is either too long, or negative";
    }
};

It has to return a char reference and not only a char because I want to make it assignable, so for example if I have a string str I could say str[5]=’c’.
I also want to handle the exception, and not to quit the program with exit().
So if there is an exception it doesn’t return any value, the string could also be empty.
What should I return? I would make it similar to std::string but I haven’t understood very well how the one of std::string is implemented.

  • 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-29T23:40:07+00:00Added an answer on May 29, 2026 at 11:40 pm

    There’s not much point to throwing an exception if you’re going to catch it in the same block. Just have your if statement control things if you wish to return something rather than throwing the error outside of your function.

    The standard string class doesn’t do any checking whatsoever on its index so that there’s no added overhead. If an index is out of bounds you get undefined behavior, because you’re using an out of bounds index on the internal buffer.

    You can choose whatever you want to return for out of bounds indexes. Zero would be a good choice as this would indicate the end of a C-style string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm training code problems like UvA and I have this one in which I
I have a training set that has input and outputs in this way: Input:
I was reading Microsoft's Class Room Training Materil. I read the following Unboxing Unboxing
I have a table Training that has a column CreatedBy which has a referential
I have a string of type ishan,training I want to split the string after
I am in the training of web developement.I have seen in most of the
I have been asked to find training resources to bring engineers up to speed
I'm designing a training program in C++ that will be distributed to a large
We have a sports training camp which is regularly attended by various teams in
I have a set of training data consisting of 20 multiple choice questions (A/B/C/D)

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.