By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,672 Members | 1,644 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,672 IT Pros & Developers. It's quick & easy.

problem with testing method with STL

P: n/a
Hello!
I have two classes called Handle which is a template class and a class
Integer which is not a template class. The Integer class is just a wrapper
class for a
primitive int with some methods. I don't show the Integer class because it
will not add any information to my problem. Main is using some STL function.
In main you put in nodes in the STL list and you can display the STL list
and other things such as erase some value from the list and find a selected
value from the list and assign a list to another list and some more.
I show the complete Handle class and the whole file with main below.

In the file where main is located is there a function that is called
copyList. The definition looks like.
void copyList(list<handle_t>* myList1, list<handle_t>* myList2)
{ *myList2 = *myList1; }
this copies the whole body of the list so they will point to the same
wrapper object(Integer class object).

if I add 1,2,3 to myList1 and then call copyList the same number will be
written for myList1 and myList2
if I then remove 2 from myList1 then myList1 will contain 1 and 3 myList2
will still contain 1,2 and 3.
if I add 4 to myList1 then myList1 will contain 1,3 and 4 myList2 will still
contain 1,2 and 3. All these are correct.

So in this example Integer object with number 1 and 3 is being shared
between myList1 and myList2..

Now to my question and problems !
I want to check that deep copy is working.
I have one method in the Handle class which have this definition
T* operator->()
{
if (*ref_count > 1)
{
(*ref_count)--;
body = new T(*body);
ref_count = new int(1);
}
return body;
}
this means that a new Integer object will be created if there are more then
1 reference to the the current Integer object.(ref_count > 1)
How do I do for testing this operator-> to see that a new Integer object is
being created.

I don't know how beacause when all these object are in the STL list you
can't access them

Many thanks

//Tony

*************C O D E S E C T I O N**********
I show the definition of the Handle class here.
#include "integer.h"
#include <iostream>
using namespace std;

template<class T>
class Handle
{
public:
Handle()
{
body = new T(0);
ref_count = new int(1);
}

Handle(T* body_ptr) //Constructor
{
body = body_ptr;
ref_count = new int(1);
}

~Handle() //Destructor
{
(*ref_count)--;
if (!*ref_count)
deleteAll();
}

T operator*() const
{ return *body; }

T& operator*()
{ return *body; }

T* operator->()
{
if (*ref_count > 1)
{
(*ref_count)--;
body = new T(*body);
ref_count = new int(1);
}
return body;
}

T* operator->() const
{ return body; }

bool operator==(const T& h)
{ return *body == h; }

bool operator==(const Handle& h)
{ return *body == *h.body; }

Handle(const Handle& h) //Copy construktor
{
body = h.body;
ref_count = h.ref_count;
(*ref_count)++;
}

int getRefCount()
{ return *ref_count; }

void operator()(Handle& h) //Funktionsanropsoperator
{ cout << *h.body << endl; }

const Handle& operator=(const Handle& h) //Tilldelningsoperator
{
if (this != &h)
{
(*ref_count)--;
if (!*ref_count)
deleteAll();

ref_count = h.ref_count;
body = h.body;
(*h.ref_count)++;
}
return *this;
}

private:
T* body;
int* ref_count;
void deleteAll()
{
delete body;
body = NULL;
delete ref_count;
ref_count = NULL;
}
};

//Start of file with main
*****************
#include "handle.h"
#include <list>
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;
typedef Handle<Integer> handle_t;

static int getValue(string text)
{
int value;
cout << endl << text << " : ";
while (!(cin >> value) || cin.peek() != '\n')
{
cin.clear();
for (; cin.peek() != EOF && cin.get() != '\n' ;)
;
cout << "Du har inte angivit nagon siffra, forsok igen" <<
endl;
cout << "Ange alternativ: ";
}
fflush(stdin);
return value;
}

//************************
int huvudMeny()
{
cout << "VALKOMMEN TILL STL HANTERING" << endl << endl;
cout << "0. => EXIT" << endl << endl;;

cout << " 1. Lagg in noder i STL_listan" << endl;
cout << " 2. Skriv ut STL_listan" << endl;
cout << " 3. Ta bort ett specificerat varde i STL listan" << endl;
cout << " 4. Ta bort samtliga noder i STL listan" << endl;
cout << " 5. Hitta ett specificerat varde i STL listan" << endl;
cout << " 6. Testa da body objektet ar delat" << endl;
cout << " 7. Testa att kopiera hela STL listan" << endl;
return getValue("Ange alternativ: ");
}
//*******************************
void dummy()
{
string dummy;
cout << "Tryck pa valfri tangent for att fortsatta" << endl;
cin >> dummy;
}
//************************
void displaySTL(list<handle_t>* myList1, list<handle_t>* myList2)
{
if (myList1->size())
{
cout << endl << "Lista 1 har foljande innehall" << endl << endl;
for_each(myList1->begin(), myList1->end(), handle_t());
cout << endl;
}
else
cout << endl << "Lista 1 ar tom" << endl << endl;

if (myList2->size())
{
cout << endl << "Lista 2 har foljande innehall" << endl << endl;
for_each(myList2->begin(), myList2->end(), handle_t());
cout << endl;
}
else
cout << endl << "Lista 2 ar tom" << endl << endl;

dummy();
}

//************************
void findSelectedValue(list<handle_t>* myList)
{
int value = getValue("Ange vardet som du vill hitta i listan: ");

if (find(myList->begin(), myList->end(), Integer(value)) !=
myList->end())
cout << endl << "Vardet " << value << " fanns i STL-listan" <<
endl << endl;
else
cout << endl << "Vardet " << value << " finns inte i listan" <<
endl << endl;

dummy();
}

//************************
bool existValue(list<handle_t>* myList, int value)
{
if (find(myList->begin(), myList->end(), Integer(value)) !=
myList->end())
return true;
else
return false;
}

//************************
void deleteAll(list<handle_t>* myList)
{
myList->clear();
cout << endl << "Alla noder ar nu borttagna" << endl << endl;
dummy();
}

//************************
void deleteSelectedValue(list<handle_t>* myList)
{
int value = getValue("Ange vardet som du vill ta bort fran listan: ");
handle_t temp( new Integer(value));
if (existValue(myList, value))
{
myList->erase(remove(myList->begin(), myList->end(), temp),
myList->end());
cout << endl;
for_each(myList->begin(), myList->end(), handle_t());
cout << endl;
}
else
cout << endl << "Vardet fanns inte i listan!!" << endl << endl;

dummy();
}

//************************
void addNodeToSTL(list<handle_t>* myList)
{
handle_t myh(new Integer(getValue("Ange vardet som du vill lagga in i
listan/listorna: ")));
myList->push_front(myh);
}

//************************
void copyList(list<handle_t>* myList1, list<handle_t>* myList2)
{ *myList2 = *myList1; }

//************************
int main()
{
list<handle_t>* myList1 = new list<handle_t>;
list<handle_t>* myList2 = new list<handle_t>;

do
{
system("cls");

switch (huvudMeny())
{
case 0: return 0;
case 1: addNodeToSTL(myList1);
break;
case 2: displaySTL(myList1, myList2);
break;
case 3: deleteSelectedValue(myList1);
break;
case 4: deleteAll(myList1);
break;
case 5: findSelectedValue(myList1);
break;
case 6: testDeepCopy(myList1);
break;
case 7: copyList(myList1, myList2);
break;
}
} while(1);

return 0;
}
Jul 23 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.