Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
cpp template samples and tutorials       Share
2008-09-28 |  RatheeshTR  | Viewed: 112  |    0

#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:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software Engineer
Tech: C ,Cpp
Send Mail: ratheesh



Related topics
Cpp - Access a Class Member Function Without Creating a Class Object
Cpp - Writing Macro Definition
Cpp - using namespace std
Cpp - Using enum
Cpp - Pointers in C++
Cpp - How to use DLL file in the C++ Program
Cpp - creating and Linking DLL Files to C++ program
Cpp - 1) vector method and iterator (simple Example)
Cpp - 2) Sort and Binary search
Cpp - 3) Vector and Copy vector
Cpp - 4) for_each ( Vector )
Cpp - 5) List and Iterator
Cpp - 6) list and Vector example.2
Cpp - 7) example"fill, copy, list" statement
Cpp - 8) list and set

Related References
cpp - operator overloading
cpp - default arguments
cpp - c++ interview tips
cpp - virtual destructors
cpp - c++ destructors
cpp - csc interview questions
object oriented programming paradigm
elements of object oriented programming
basic concepts of oops and structure of c++ program
exception (runtime error) handling - cpp
cpp function pointers
cpp - access specifiers between base and derived classes
cpp - inautix interview questions
comparing popular programming languages

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us