Hi, all.
I have one question: I am writing my-string class now according to the assignment. And like other String class assignment, we have to overload the operator, do the difference type constructor, etc. Well, i almost finish my assignment. But there have one problem with my code.
I am not allowed to use strcpy() and strcat() fuction in my code cause it will cause NULL Terminator. So i changed them to strcpy_s() and strcat_s() which i still not allowed to.
So now i am became crazy, here is one part of my code and i was thinking maybe someone could help me figure out that.(is that use loop will avoid NULL Terminator???)

String& String::operator+=(char* other)  
{  
        char* temp;
	m_length=strlen(m_buffer)+strlen(other)+1;//m_length and m_buffer in my String class private elements
	temp=new char[m_length];
  	//strcpy_s(temp,m_length,m_buffer);//this one i am not allowed to use now.
 
	//strcat_s(temp,m_length,other);//this one i am also not allowed to use now.
	char *t=m_buffer;
	m_buffer=temp;
	delete []t;
	return *this; //my code
}

thanks la!!

Just write your own loop to copy chars without the null-terminator?
However, on this line:

m_length = [B]strlen[/B](m_buffer) + [B]strlen[/B](other) + 1;

You are using a function which relies on the null-terminator at the end of the string, so you will have to keep track of the string length yourself while adding characters to it. Something like this perhaps?

String& String::operator +=(char* other)   {
  // Temporarily store the old length (for copying chars)
  size_t oldLength = m_length;

  // New size of string
  m_length += strlen(other);

  // Allocate enough space for the new string
  char *new_str = new char[m_length];

  // Copy string without null-terminator
  for (size_t i = 0; i < oldLength; ++i)
    new_str[i] = m_buffer[i];

  // Delete old chars
  delete[] m_buffer;

  // Assign new string
  m_buffer = new_str;

  return *this;
}

Hope this helps.

hey, the function you give me is actually operator = right??
Cause what i am posting is operator += which also have strcat() function.
Thanks.

Sorry, my mistake :P

String& String::operator +=(char* other)   {
  // Temporarily store the old length (for copying chars)
  size_t oldLength = m_length;

  // New size of string
  m_length += strlen(other);

  // Allocate enough space for the new string
  char *new_str = new char[m_length];

  // Copy string without null-terminator
  for (size_t i = 0; i < oldLength; ++i)
    new_str[i] = m_buffer[i];

  for (size_t i = oldLength, j = 0; i < m_length; ++i, ++j)
    new_str[i] = other[i];

  // Delete old chars
  delete[] m_buffer;

  // Assign new string
  m_buffer = new_str;

  return *this;
}

Haven't really tried compiling this, but I think it's right this time.

commented: good, expert, kind +1

William Hensworth's code declares memory for the new string and copies the char from the original string into the new string, but it doesn't copy the char from other into the new string. You will need to add another loop to do that, but it's only a slightly different version of lines 12 and 13, so I'm sure you can figure it out. When you're done copying other into new_str, then you need reassign new_str to m_buffer as William Hemsworth did.

Edit: I need to remember to recheck each time before I post. Sorry.

William Hemsworth
thanks.

But when i did:

String str1;
String str2;
str1+=str2;//which is fine
(str1+=str2)+=str1;//which have some strange output??

thanks.

figure it out.
thanks.

In case anybody else tries it.

for (size_t i = oldLength, j = 0; i < m_length; ++i, ++j)
  new_str[i] = other[[B]j[/B]];

I don't know what's wrong with me today, two mistakes in one thread :icon_redface:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.