Click here to Skip to main content
12,759,231 members (33,099 online)
Click here to Skip to main content

Stats

57.7K views
584 downloads
14 bookmarked
Posted 22 Aug 2005

Clone Smart Pointer (clone_ptr)

, 23 Aug 2005 Public Domain
A non-sharing smart pointer class that can be used with STL containers like std::map, vector, list, set, and deque. The smart pointer has assignment operator and greater than operator that call the target object's equal operator.
ClonePointerDemo
VC60
ClonePointerDemo.dsp.src
ClonePointerDemo.dsw
res
ClonePointerDemo.ico
ClonePointerDemo.dsp
VC71
res
ClonePointerDemo.ico
ClonePointerDemo.dsp
ClonePointerDemo.dsp.src
ClonePointerDemo.dsw
// ClonePointerDemo.cpp : Defines the entry point for the console application.
//

#pragma warning(disable: 4786) 

#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <algorithm>
#include <functional>
using namespace std;

#include "clone_ptr.h"

struct BaseClass 
{ 
    std::string m_strData; 
    BaseClass(const char *n) : m_strData(n) {} 
	virtual ~BaseClass(){}
	virtual void WhoAmI(){cout << "I'm BaseClass " << m_strData << endl;}
    bool operator<(const BaseClass &rhs){return m_strData < rhs.m_strData;} 
	bool operator==(const BaseClass& rhs)const{return (m_strData == rhs.m_strData);}
}; 

struct Derived_A : public BaseClass
{
	Derived_A(const char *n):BaseClass(n){}
	void WhoAmI(){cout << "I'm Derived_A " << m_strData << endl;}
};

struct Derived_B : public BaseClass
{
	Derived_B(const char *n):BaseClass(n){}
	void WhoAmI(){cout << "I'm Derived_B " << m_strData << endl;}
};

void VectorSortDemo()
{
	std::vector<clone_ptr<BaseClass> > vBaseClass; 
	//Setup data using base and derived type classes
	vBaseClass.push_back(new BaseClass( "3" )); 
	vBaseClass.push_back(new Derived_B( "2" )); 
	vBaseClass.push_back(new BaseClass( "1" )); 
	vBaseClass.push_back(new Derived_A( "5" )); 
	vBaseClass.push_back(new BaseClass( "4" )); 

	//Sort Data
	sort(vBaseClass.begin(), vBaseClass.end()); 

	//Display sorted data
	for (size_t i = 0;i < vBaseClass.size();++i)
	{
		vBaseClass[i]->WhoAmI();
	}
	cout << "----------------------------------" << endl;
	cout << "----------------------------------" << endl;
}

void SetDemo()
{
	std::set<clone_ptr<BaseClass> > sBaseClass; 
	//Setup data using base and derived type classes
	sBaseClass.insert(new BaseClass( "3" )); 
	sBaseClass.insert(new Derived_B( "2" )); 
	sBaseClass.insert(new BaseClass( "1" )); 
	sBaseClass.insert(new Derived_A( "5" )); 
	sBaseClass.insert(new BaseClass( "4" )); 

	//Display sorted data
	for(std::set<clone_ptr<BaseClass> >::iterator i = sBaseClass.begin();i!=sBaseClass.end();++i)
	{
		(*i)->WhoAmI();
	}
	cout << "----------------------------------" << endl;
	cout << "----------------------------------" << endl;
}

void RemoveFromListDemo()
{
	std::list<clone_ptr<BaseClass> > lBaseClass; 
	lBaseClass.push_back(new BaseClass( "X" )); 
	lBaseClass.push_back(new Derived_B( "2" )); 
	lBaseClass.push_back(new BaseClass( "X" )); 
	lBaseClass.push_back(new Derived_A( "5" )); 
	lBaseClass.push_back(new BaseClass( "4" ));

	//Remove certain values
	lBaseClass.remove(clone_ptr<BaseClass>(new BaseClass("X"))); //Remove all X's
	
	//Display results
	for(std::list<clone_ptr<BaseClass> >::iterator i = lBaseClass.begin();i!=lBaseClass.end();++i)
	{
		(*i)->WhoAmI();
	}
	cout << "----------------------------------" << endl;
	cout << "----------------------------------" << endl;
}

void ContainerEqualityDemo()
{
	list<clone_ptr<BaseClass> > PtrsX;
	PtrsX.push_back(new BaseClass("1"));
	PtrsX.push_back(new BaseClass("2"));
	PtrsX.push_back(new BaseClass("3"));
	
	list<clone_ptr<BaseClass> > PtrsY;
	PtrsY.push_back(new Derived_B("4"));
	PtrsY.push_back(new Derived_B("5"));
	PtrsY.push_back(new Derived_B("6"));
	
	list<clone_ptr<BaseClass> > PtrsZ;
	PtrsZ.push_back(new Derived_A("1"));
	PtrsZ.push_back(new Derived_A("2"));
	PtrsZ.push_back(new Derived_A("3"));
	
	bool IsEqual1 = (PtrsX == PtrsY); //Should be false
	bool IsEqual2 = (PtrsX == PtrsZ); //Should be true
	
	cout << "Should be false (0) IsEqual1 = " << IsEqual1 << endl;
	cout << "Should be true (1) IsEqual2 = " << IsEqual2 << endl;
	cout << "----------------------------------" << endl;
	cout << "----------------------------------" << endl;
}

void CopyCorrectDerivedTypeDemo()
{
	std::vector<clone_ptr<BaseClass> > vBaseClass; 
	//Setup data using base and derived type classes
	vBaseClass.push_back(new BaseClass( "3" )); 
	vBaseClass.push_back(new Derived_B( "2" )); 
	vBaseClass.push_back(new BaseClass( "1" )); 
	vBaseClass.push_back(new Derived_A( "5" )); 
	vBaseClass.push_back(new BaseClass( "4" )); 
	
	//Copy contents from one container to another
	std::vector<clone_ptr<BaseClass> > vBaseClass_Copy(vBaseClass.begin(), vBaseClass.end()); 
	
	//Display results
	for (size_t i = 0;i < vBaseClass_Copy.size();++i)
	{
		vBaseClass_Copy[i]->WhoAmI();
	}
	cout << "----------------------------------" << endl;
	cout << "----------------------------------" << endl;
}

int main(int , char*)
{
	VectorSortDemo();
	SetDemo();
	RemoveFromListDemo();
	ContainerEqualityDemo();
	CopyCorrectDerivedTypeDemo();
	system("pause");
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

Share

About the Author

Axter
Software Developer (Senior) Commvault
United States United States
I started programming as a hobby in 1985 on my 8-bit Atari computer. I programmed in BASIC and in assembly language until I upgraded to a 16-bit IBM compatible in 1988.
In 1989, I started programming in C using Turbo C++. IMHO, Turbo C/C++ was the best compiler available to the market for many years, until VC++ 4.0 came out.
I started using MFC with VC++ 4.0 in 1996, and I developed and sold an application called GameMenu95. It had limited success, and I retired the program in 1999.

I now perform contract work in a variety of operating systems mostly using C/C++.
I occasionally have contract work for Java applications and MS Access development.

My favorite language of choice is C++. I believe it has the best combination of flexibility, speed, and type safety.
I’ve used many compilers and IDE, to include (VC++7.0, 7.1, 6.0, GCC 2.x, 3.x, Turbo C/C++, Borland 5.x, Watcom, MinGW, DevC++, Eclipse….)
However, by far, my most favorite IDE is VC++6.0. Although this compiler has poor C++ compliance, it more then makes up for it by its outstanding interface and tools.

Coding is still my hobby, and I’m very fortunate in that I get paid good money to engage in my hobby!

Life is good!!! Smile | :)


If I really helped you save some bucks, I would like to ask you for something: Please donate some of these saved bucks (you decide) to the charity organization of your choice - or just help out somebody who is needier than you are......
Pass it on....... Smile | :)

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 23 Aug 2005
Article Copyright 2005 by Axter
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid