user defined string::reserve()
<p>So we are writing a bunch of user defined functions that mimic the string class member functions. I am stuck on reserve. It looks like it should work but I am missing something. I have tried a couple of diffrent variations such as putting this-> in front of the member varibles or mystring:: infront of them... my out put does not change but throught cout statment placed within the function I know it access the function. </p>

<p>here is the code</p>

<pre><code>#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include "mystring.h"
#define string mystring

using namespace std;


void check (string s, string name){
    cout &lt;&lt; "checking " &lt;&lt; name &lt;&lt; endl;
    cout &lt;&lt; name &lt;&lt; " contains " &lt;&lt; s &lt;&lt; endl;
    cout &lt;&lt; name &lt;&lt; " capacity() is " &lt;&lt; s.capacity() &lt;&lt; endl;
    cout &lt;&lt; name &lt;&lt; " length() is " &lt;&lt; s.length() &lt;&lt; endl;
    cout &lt;&lt; name &lt;&lt; " size() is " &lt;&lt; s.size() &lt;&lt; endl;
    cout &lt;&lt; name &lt;&lt; " max_size() is " &lt;&lt; s.max_size() &lt;&lt; endl &lt;&lt; endl;

}

int main(int argc, char** argv) {

    cout&lt;&lt;"This is Lab 5"&lt;&lt;endl;

    string s1("Hello, World!");//step 2
    string s1name("s1");//step 3

    check(s1,s1name);//step 4
    check(s1,s1name);//step 5

    cout &lt;&lt; "---Testing assignment operator---\n\n";
{
string s2;
s2=s1;
string s2name("s2");
check(s2,s2name);
if(s1==s2)
    cout&lt;&lt;"comparison true\n";
else 
    cout&lt;&lt;"comparison false\n";
}

    check(s1,s1name);



    string s3("check assignment");
    s3=s3;//checking to see if operator= is used when they are the same object. 

    check(s3,"s3");

    cout&lt;&lt;"Lab 5 ends"&lt;&lt;endl;//step6



//    //clear check
//    s3.clear();
//    check(s3,"s3");


//    if(s1==s3)
//        cout&lt;&lt;"comparison true\n";
//    else
//        cout&lt;&lt;"comparison false\n";

// reserve check
//    mystring::size_type res;
//    res=40;

    s3.reserve(40);//still working on reserve
    check(s3,"s3");

    cout&lt;&lt;"in main buf size"&lt;&lt;s3.capacity()&lt;&lt;endl;

    s3.reserve(5);
    check(s3,"s3");



//    char* test=s3.begin();
//    cout&lt;&lt;&amp;test&lt;&lt;endl;
//    cout&lt;&lt;&amp;s3&lt;&lt;endl;


//empty check
//    string s4;
//    
//    if (s4.empty())
//        cout&lt;&lt;"Empty is true\n";
//    else 
//        cout&lt;&lt;"Empty is false\n";







    return 0;
}





#ifndef MYSTRING_H
#define MYSTRING_H

#include &lt;iostream&gt;
#include &lt;math.h&gt;
#include &lt;cstring&gt;

using namespace std;

class mystring {
public:
    // types with scope in this class
    typedef unsigned int size_type;
    typedef char * iterator;
    typedef const char * const_iterator;
    static const long int npos = 1073741824;

    // default constructor
    mystring();//good
    // other constructors
    mystring(const char *);//good
    // copy constructor
    mystring(const mystring&amp; orig);//

    // destructor
    ~mystring();////

    // iterators

    iterator begin();//good
    iterator end();//good

    //=== memory related ===

    // change buffer size to n
    void reserve(size_type n);

    size_type size() const;////good returns len
    size_type length() const;////good returns len
    size_type capacity() const;////good returns buf_size
    size_type max_size() const;////good
    bool empty() const;////good

    //=== overloading operators ===

     // assignment operator
    mystring&amp; operator=(const mystring&amp;);////
//    mystring&amp; operator=(const char *);

    // array notation

    char operator[](size_type pos) const;
    char&amp; operator[](size_type pos);

    // append

    mystring&amp; operator+=(const mystring&amp; str);
    mystring&amp; operator+=(const char * str);

    //=== methods that modifiy the string

    void clear();////good

    void push_back(char c);


    mystring&amp; append(const mystring&amp; str);
    mystring&amp; append(const char * str);

    mystring&amp; insert(size_type pos, const mystring&amp; str);
    mystring&amp; insert(size_type pos, const char * str);

    mystring&amp; replace(size_type start, size_type span, const mystring&amp; str);
    mystring&amp; replace(size_type start, size_type span, const char * str);

    //=== conversion to c string

    const char * c_str() const;//


private:
    // pointer to the memory location where string is stored as a c-style
    // string
    char * ptr_buffer;
    // the size of the memory in terms of bytes or characters capable of going into it currently
    size_type buf_size;
    // number of characters currently in the memory not including the
    // terminating null character
    size_type len;


};




#include "mystring.h"

// default constructor provided for lab 5
mystring::mystring() {
    ptr_buffer = new char[1];
    *ptr_buffer = '\0';
    buf_size = 1;
    len = 0;
}

// constructor from c-style string or "abc" provided for lab 5
mystring::mystring(const char * s) {
len = strlen(s);
buf_size = len + 1;
ptr_buffer = new char[buf_size];
strcpy(ptr_buffer, s);

}

// copy constructor to be implemented in lab 5
mystring::mystring(const mystring&amp; orig) {

    len=orig.length();
    ptr_buffer=new char[len+1];
    buf_size=len+1;

    for(int n=0 ;n&lt;buf_size; n++ )
    {
        ptr_buffer[n]=orig.ptr_buffer[n];
    }
    ptr_buffer[buf_size]='\0';
}



void mystring::reserve(size_type n)
{
    cout&lt;&lt;"cccccc:"&lt;&lt;capacity()&lt;&lt;endl;
    if( n &gt; capacity() )
{
    const char* temp = ptr_buffer;
    ptr_buffer = new char[n];
    memcpy(ptr_buffer, temp, len+1);
    delete [] temp;
    buf_size=n;
    cout&lt;&lt;"bbbbbuf size"&lt;&lt;buf_size&lt;&lt;endl;
}



//    char *temp;
//    
//    temp=new char[n];
//    
//    int i=0;
//    
//    for(;i&lt;=len;i++)
//    {
//        temp[i]=ptr_buffer[i];
//        
//    }
//    buf_size=n;
//    
//    delete [] ptr_buffer;
//    
//    ptr_buffer=temp;
//    

}







mystring::iterator mystring::begin()//think is working correctly
{
    iterator it=ptr_buffer;

    return it;
}

mystring::iterator mystring::end()//think is working correctly
{
    iterator it=ptr_buffer+len;

    return it;
}



// one of the over loaded assignment operator to be implemented // assignment 3 (or for lab 5 if you have more time)
mystring&amp; mystring::operator=(const mystring&amp; orig){
    if(this!=&amp;orig)
//    {
//        cout&lt;&lt;"this==&amp;mystring  if statment activated\n";//comment out after testing
//        break;
//    }
    {
    delete this-&gt;ptr_buffer;

    this-&gt;len=orig.len;//length();
    this-&gt;ptr_buffer=new char((this-&gt;len)+1);
    this-&gt;buf_size=(this-&gt;buf_size)+1;
    cout&lt;&lt;"Using assignment operator="&lt;&lt;endl;
    for(int n=0;n&lt;this-&gt;buf_size;n++)
    {
        this-&gt;ptr_buffer[n]=orig.ptr_buffer[n];
    }
    this-&gt;ptr_buffer[buf_size]='\0';
    return *this;

    }
}

// some simple methods provided for lab 5
 mystring::size_type mystring::size() const {
     return len;
 }
 mystring::size_type mystring::length() const{
     return len;
 }
 mystring::size_type mystring::capacity() const{
       return buf_size;
 }
 mystring::size_type mystring::max_size() const{
       return (int)pow(2,30) -4 ;
 }




 bool mystring::empty() const 
 {
     if(len==0)
         return true;
     else
         return false;
 }
// destructor to free space implemented for lab 5
mystring::~mystring() {
    delete [] ptr_buffer;
}
// provided for lab 5 so we may cout mystring
ostream&amp; operator&lt;&lt;(ostream&amp; out, const mystring&amp; str) {
    out &lt;&lt; str.c_str();
    return out;
}
// provided for lab 5 to support the implementation of &lt;&lt;
const char * mystring::c_str() const {
    return ptr_buffer;
}



char mystring::operator[](size_type pos) const
{
    return *(ptr_buffer+pos);



}




void mystring::clear()
{
    char *temp;
    temp=new char[1];
    temp[0]='\0';

    ptr_buffer=temp;


    buf_size=0;
    len=0;
}




 void mystring::push_back(char c)
 {





 }







bool operator==(const mystring&amp; lhs, const mystring&amp; rhs)
{
   if(lhs.length()==rhs.length()) 
   {
       for(int i=0; i&lt;lhs.length();i++)
       {
           if(lhs[i]!=rhs[i])
               return false;

       }
       return true;
   }

   return false;

}
</code></pre>

<p>So I put some cout statements in the reserve() function and in main. The reserve functions is changing the buffer size and capacity call is returning correctly but when we use the check function it is not showing that any change has occurred. I have put in revised code. </p>

<p>Ok, everyone. Turns out on the check function declared before main that it makes a copy of the mystring object. The copy constructor was defined to make buf_size= len + 1. So I turned it into buf_size=orig.buf_size. the reserve function probably worked as stated and yes it needed an if statment to make sure we were not shrinking it, but i was not worried about that yet. I used daves example with a little added code. thanks all for your help. </p>
