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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:01:37+00:00 2026-05-14T23:01:37+00:00

I’m trying to write my own C++ String class for educational and need purposes.

  • 0

I’m trying to write my own C++ String class for educational and need purposes.
The first thing is that I don’t know that much about operators and that’s why I want to learn them.
I started writing my class but when I run it it blocks the program but does not do any crash.
Take a look at the following code please before reading further:

class CString
{
private:
  char* cstr;
public:
  CString();
  CString(char* str);
  CString(CString& str);
  ~CString();

  operator char*();
  operator const char*();
  CString operator+(const CString& q)const;
     CString operator=(const CString& q);
};

First of all I’m not so sure I declared everything right. I tried googleing about it but all the tutorials about overloading explain the basic ideea which is very simple but lack to explain how and when each thing is called. For instance in my = operator the program calls CString(CString& str); but I have no ideea why.
I have also attached the cpp file below:

CString::CString()
{
 cstr=0;
}
CString::CString(char *str)
{
 cstr=new char[strlen(str)];
 strcpy(cstr,str);
}
CString::CString(CString& q)
{
 if(this==&q)
  return;
 cstr = new char[strlen(q.cstr)+1];
 strcpy(cstr,q.cstr);
}
CString::~CString()
{
 if(cstr)
  delete[] cstr;
}
CString::operator char*()
{
 return cstr;
}
CString::operator const char* ()
{
 return cstr;
}
CString CString::operator +(const CString &q) const
{
 CString s;
 s.cstr = new char[strlen(cstr)+strlen(q.cstr)+1];
 strcpy(s.cstr,cstr);
 strcat(s.cstr,q.cstr);
 return s;
}
CString CString::operator =(const CString &q)
{
 if(this!=&q)
 {
  if(cstr)
   delete[] cstr;
  cstr = new char[strlen(q.cstr)+1];
  strcpy(cstr,q.cstr);
 }
 return *this;
}

For testing I used a code just as simple as this
CString a = CString(“Hello”) + CString(” World”);
printf(a);
I tried debugging it but at a point I get lost. First it calls the constructor 2 times for “hello” and for ” world”. Then it get’s in the + operator which is fine. Then it calls the constructor for the empty string. After that it get’s into “CString(CString& str)” and now I’m lost. Why is this happening? After this I noticed my string containing “Hello World” is in the destructor (a few times in a row). Again I’m very puzzeled. After converting again from char* to Cstring and back and forth it stops. It never get’s into the = operator but neither does it go further. printf(a) is never reached.
I use VisualStudio 2010 for this but it’s basically just standard c++ code and thus I don’t think it should make that much of a difference

  • 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-14T23:01:37+00:00Added an answer on May 14, 2026 at 11:01 pm

    The line:

    cstr=new char[strlen(str)];
    

    should be:

    cstr=new char[strlen(str) + 1];
    

    Also, the test for self-assignment does not make sense in the copy constructor – you are creating a new object – it cannot possibly have the same address as any existing object. And the copy constructor should take a const reference as a parameter,

    If in your code, you were expecting the assignment operator to be used, you would be expectining wrong. This code:

    CString a = CString("Hello") + CString(" World");
    

    is essentially the same as:

    CString a( CString("Hello") + CString(" World") );
    

    which is copy construction, not assignment. The temporary CString “Hello world” will be destroyed (invoking the destructor) after a has been constructed.

    Basically, it sounds as if your code is working more or less as expected.

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

Sidebar

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.