Implementing a copy on write String class using reference counting in C++ I have tried to implement a copy on write string class with value semantics and reference counting.

I wanted to support indexing, so I have overloaded the operator[] and to differentiate between the l-value and r-value usage it is returning a proxy object.

#include &lt;iostream&gt;
#include &lt;string.h&gt;
class proxy;

//Reference counting class.Also holds the string.
class RC
{
    public:
        RC(const char * str);
        ~RC();
    private:
        char* str;
        int count;

    friend class mystring;
    friend class proxy;

};


class mystring
{
    public:
        mystring(const char*  str);
        mystring&amp; operator=(const mystring&amp; str);
        mystring(const mystring&amp; str);
        ~mystring();

        void print() const;
        proxy operator[](int n);

    private:
        RC *pRC;

    friend class proxy;
};
class proxy
{
    public:
        proxy(mystring* r,int n);
        proxy&amp; operator=(const char&amp; c);
        proxy&amp; operator=(const proxy&amp; c);
        operator char() const;

    private:
        int n;
        mystring* ptr;
};


RC::RC(const char * str1)
:str(new char[strlen(str1)+1]),count(1)
{ 
    strcpy(str,str1);
}

RC::~RC()
{
    if(--count == 0)
        delete [] str;
}

proxy mystring::operator[](int n)
{
    return proxy(this,n);
}

mystring::mystring(const char*  str)
:pRC(new RC(str))
{
}
mystring&amp; mystring::operator=(const mystring&amp; rhs)
{
    if(this != &amp;rhs)
    {
        if(--pRC-&gt;count == 0)
            delete pRC;
        pRC = rhs.pRC;
        ++pRC-&gt;count;
    }
    return *this;
}
mystring::mystring(const mystring&amp; rhs)
{
    pRC = rhs.pRC;
    ++pRC-&gt;count;
}

mystring::~mystring()
{
    if(--pRC-&gt;count == 0)
        delete pRC;
}
void mystring::print() const
{
    std::cout &lt;&lt; pRC-&gt;str &lt;&lt; ":" &lt;&lt; pRC-&gt;count &lt;&lt; '\n';
}


proxy::operator char() const
{
    return ptr-&gt;pRC-&gt;str[n];
}

proxy::proxy(mystring* r,int n) 
:ptr(r),n(n)
{}

proxy&amp; proxy::operator=(const char&amp; c)
{

    if(c != ptr-&gt;pRC-&gt;str[n])
    {
        char cpy[strlen(ptr-&gt;pRC-&gt;str)+1];
        strcpy(cpy,ptr-&gt;pRC-&gt;str);
        ptr-&gt;pRC-&gt;~RC();
        cpy[n] = c;
        ptr-&gt;pRC = new RC(cpy);
    }

    return *this;
}


proxy&amp; proxy::operator=(const proxy&amp; rhs)
{
    if(this != &amp;rhs)
    {
        *this = (char)rhs;

    }   

    return *this;
}


Can you please help me improve the code?
Code simplification

mystring acts essentially as a smart pointer. It's already implemented in the standard library. You can use the std::shared_ptr class. This way, mystring can just have an std::shared_ptr&lt;std::string&gt; as a member. The default copy constructor, assignment operator and the desturctor will work properly. 

Use of non-standard features

char cpy[strlen(ptr-&gt;pRC-&gt;str)+1]

variable sized arrays are not a part of the standard C++. Don't use them. If you need it, you should allocate it dynamically. 

If you use the std::shared_ptr&lt;std::string&gt; as I said above, it'll be even simpler as std::string has a proper copy constructor.  

Index

It's conventional to use size_t, not int as an index type (at least that's how an std::string works).