cpp template samples and tutorials
Share
#include <iostream.h>
#include <string.h>
template<class T>
class RCSmartPointer
{
public:
RCSmartPointer(T* ptrToRC = 0);
RCSmartPointer(const RCSmartPointer& rhs);
~RCSmartPointer();
RCSmartPointer& operator=(const RCSmartPointer& rhs);
T* operator->() const;
T& operator*() const;
private:
T * pRC;
void initialize();
};
template<class T>
void RCSmartPointer<T>::initialize()
{
if ( pRC == 0 )
return;
if ( pRC->isShareable() == false )
{
pRC = new T(*pRC);
}
pRC->addRef();
}
template<class T>
RCSmartPointer<T>::RCSmartPointer(T* ptrToRC):
pRC(ptrToRC)
{
initialize();
}
template<class T>
RCSmartPointer<T>::RCSmartPointer(const RCSmartPointer& rhs):
pRC(rhs.pRC)
{
initialize();
}
template<class T>
RCSmartPointer<T>::~RCSmartPointer()
{
if ( pRC )
pRC->removeRef();
}
template<class T>
RCSmartPointer<T>& RCSmartPointer<T>::operator=(const RCSmartPointer& rhs)
{
if ( pRC == rhs.pRC )
return *this;
if ( pRC )
pRC->removeRef();
pRC = rhs.pRC;
return *this;
}
template<class T>
T* RCSmartPointer<T>::operator->() const
{
return pRC;
}
template<class T>
T& RCSmartPointer<T>::operator*() const
{
return *pRC;
}
class ReferenceCounted
{
public:
void addRef() { ++referenceCount; }
void removeRef() {
if ( --referenceCount == 0 )
delete this;
}
void markNotShareable() { shareable = false; }
bool isShareable() { return isShareable; }
bool isShared() { return referenceCount > 1; }
protected:
ReferenceCounted():referenceCount(0), shareable(true) {}
ReferenceCounted(const ReferenceCounted& rhs):referenceCount(0), shareable(true) {}
ReferenceCounted& operator=(const ReferenceCounted& rhs) { return *this; }
virtual ~ReferenceCounted(){}
private:
int referenceCount;
bool shareable;
};
class String
{
public:
String(const char * cString = ""):rcString(new countedString(cString)){}
char operator[](int index) const { return rcString->cString[index]; }
char& operator[](int index);
operator char*() const { return rcString->cString; }
void ShowCountedStringAddress() const { rcString->ShowAddress(); }
private:
struct countedString : public ReferenceCounted
{
char * cString;
countedString( const char * initialCString) { initialize(initialCString); }
countedString( const countedString & rhs) { initialize(rhs.cString); }
~countedString()
{
delete [] cString;
}
void initialize(const char * initialCString)
{
cString = new char[strlen(initialCString) +1];
strcpy(cString,initialCString);
}
void ShowAddress() const { cout << &cString; }
friend class RCSmartPointer<countedString>;
};
RCSmartPointer<countedString> rcString;
};
char & String::operator[](int index)
{
if ( rcString->isShared())
{
rcString->removeRef();
rcString = new countedString(rcString->cString);
}
rcString->markNotShareable();
return rcString->cString[index];
}
int main()
{
String s1("hello world");
String s2 = s1; // copy constructor
String s3("bye");
s3 = s2; // assignment
cout << "s1: " << s1 << " ";
s1.ShowCountedStringAddress();
cout << "\ns2: " << s2 << " ";
s2.ShowCountedStringAddress();
cout << "\ns3: " << s3 << " ";
s3.ShowCountedStringAddress();
cout << endl;
return 0;
}
Comments:
|
Submitted By:
Prof: Software Engineer
Tech: C ,Cpp
|