Hi!
I'm trying to make a list to work! i have following classes:
Concept.h
Code:
#include <string>
class Concept
{
public:
Concept();
//Copy contructor
Concept(const Concept& c);
~Concept();
...
private:
char *name;
char *explaination;
....
};
Concept.cc
Code:
#include "Concept.h"
//////////////////////////////////////////////////
#include <iostream>
using std::cout;
//////////////////////////////////////////////////
//Contructor, called at initation
Concept::Concept()
{
//all empty
name = NULL;
explaination = NULL;
page=0;
nameSet = false;
explainationSet = false;
}
//Copy contructor
Concept::Concept(const Concept& c)
{
//copy name
name = new char[strlen(c.name) + 1];
strcpy(name, c.name);
//copy explaination
explaination = new char[strlen(c.explaination) + 1];
strcpy(explaination, c.explaination);
//copy page reference
page = c.page;
nameSet = true;
explainationSet = true;
}
Node.h
Code:
#include "Concept.h"
using std::ostream;
typedef Concept ListItemType;
class Node
{
public:
//Contructor, called at initation
Node();
//Destructor, called at finalisation
~Node();
//Sets the item for Node
void setItem(ListItemType anItem);
//Gets the item of Node
ListItemType getItem();
...
private:
ListItemType item;
Node* pNext;
};
Node.cc
Code:
#include "Node.h"
#include <iostream>
using std::cout;
//Contructor, called at initation
Node::Node()
{
pNext = NULL;
}
//Destructor, called at finalisation
/Node::~Node()
{
//remove allocated memory
}
//Set the item for Node
void Node::setItem(ListItemType anItem)
{
item = ListItemType(anItem);
}
//Gets the item of Node
ListItemType Node::getItem()
{
return ListItemType(item);
}
...
NOW, the problem is when the
Node::setItem() is called the object get copied by the
copy costructor but when it leaves the method it destroys it. why is this? The same thing happens with the method
Node::getItem() ????
I though the copy contructor takes care of it and the object should be destroyed when the object
Node is destroyed.
any help would be appreciated. thanks
indy