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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:18:02+00:00 2026-05-25T01:18:02+00:00

I have an assignment that I need to create a custom C type string

  • 0

I have an assignment that I need to create a custom C type string class in C++. I am having trouble getting this to work. Currently my code crashes with a run time error right at the start. I also know many of my function are wrong but I want to get the member functions sorted before I go on and fix the other functions. Bear in mind that all the function prototypes were given to us and I can not change them. I need to write the ‘guts’ so to speak.

What is wrong with my constructor for a start?

#include <iostream>
#include "tstr.h"
using namespace std;

//Default constructor to initialize the string to null
TStr::TStr() {
    strPtr = 0;
    strSize = 0;
}  
//constructor; conversion from the char string
TStr::TStr(const char *str) {
    int i=0;
    while (str[i] != '/0') {
        strPtr = new char [strlen(str)+1];
        for (i=0; i <strSize;++i) {
            strPtr[i] = str[i];
        }
        ++i;
    }
    strSize = i;
} 
//Copy constructor
TStr::TStr(const TStr&) {
}
//Destructor
TStr::~TStr() {
    if (strPtr) {
        delete[] strPtr;
    }
}

//subscript operators-checks for range
char& TStr::operator [] (int i) {
    assert (i >= 0 && i < strSize);
    return strPtr[i];
}
const char& TStr::operator [] (int i) const {
    assert (i >= 0 && i < strSize);
    return strPtr[i];
}

//overload the concatenation oprerator
TStr TStr::operator += (const TStr& str) {
    //this->strPtr += str.strPtr;
    //this->strSize += str.strSize;
    return *this;
}
//overload the assignment operator
const TStr& TStr::operator = (const TStr& str) {
    if (this != &str) {
        delete[] strPtr;
        strPtr = new char[strSize = str.strSize];
        assert(strPtr);
        for (int i=0; i<strSize; ++i) {
            strPtr[i] = str.strPtr[i];
        }
    }
    return *this;
}

//overload two relational operators as member functions
bool TStr::operator == (const TStr& str) const {
    return (strPtr == str.strPtr && strSize == str.strSize);
}
bool TStr::operator < (const TStr& str) const {
    return (strPtr < str.strPtr && strSize < str.strSize);
}
//the length of the string
int TStr::size() {
    return strSize;
}

Thanks for any replies/help! 🙂

EDIT 1: Okay the constructor is now working but I am still getting a runtime error and I’m 90% sure it is to do with my overloaded += operator. It looks fine though and compiles okay. What am I missing?

(Note: Only small changes have been made to the above code, but let me know if you want to see the whole lot.)

//overload the concatenation oprerator
TStr TStr::operator += (const TStr& str) {
    for(int i = 0; i < strSize; ++i) {
        strPtr[i] += str.strPtr[i];
    }
    return *this;
}

EDIT 2: Okay this what I have now. Compiles fine but doesn’t actually add the two strings together with the += like it should. Anyone got any ideas?

//overload the concatenation oprerator
TStr TStr::operator += (const TStr& str) {
    char *buffer = new char[strSize + str.strSize + 1];
    strcpy(buffer, strPtr);
    strcat(buffer, str.strPtr);
    delete [] strPtr;
    strPtr = buffer;
    return *this;
}

//overload the assignment operator
const TStr& TStr::operator = (const TStr& str) {
    if (this != &str) {
        delete[] strPtr;
        strPtr = new char[strSize = str.strSize];
        assert(strPtr);
        for (int i=0; i<strSize; ++i) {
            strPtr[i] = str.strPtr[i];
        }
    }
    return *this;
}
  • 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-25T01:18:03+00:00Added an answer on May 25, 2026 at 1:18 am

    Your ctor is pretty much a mess.

    You use i for two different things — at the same time. You also copy the entire contents of str into strPtr once for each character in str.

    Basically, you have to decide, are you going to use the C run-time library or not?

    Using it:

    TStr::TStr(const char *str) 
    {
      strSize = strlen(str);
      strPtr = new char [strSize+1];
      strcpy(strPtr, str);
    }
    

    not using it:

    TStr::TStr(const char *str) 
    {
      int i = 0;
      while (str[i] != '\0') 
           ++i;
      strSize = i;
      strPtr = new char [i+1];
      for (i=0; i < strSize;++i)
            strPtr[i] = str[i];
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a school assignment that involves both PHP and asp.net. Now the
I have an assignment to create a GUI using MATLAB GUIDE and am having
I have a wierd problem that i need to work out how to resolve,
I'm working on an assignment that is telling me to assume that I have
I have an assignment in a language-independent class, and part of it is using
I have a moderately simple assignment, to create a PHP/PDO site with login functionality
For an assignment I have to write a Tribool class in C# using a
Suppose I have a variable that's already been initialized to a string via an
I've this question from an assignment to create a Store which rent out books,
I have seen a codebase recently that I fear is violating alignment constraints. I've

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.