LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-10-2005, 02:38 PM   #1
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Rep: Reputation: 32
object loses data at funcyion call (c++)


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
 
Old 05-10-2005, 05:15 PM   #2
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Original Poster
Rep: Reputation: 32
sorry, i forgot to include the destructor for concept:

~Concept()
{
//clear memory
delete name;

delete explaination;
}

the ojbect stays apperantly (till Node is destroyed) but its data gets destroyed by the destructor...

sorry about that.

indy
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Fedora 3 loses .png nelsonrherron Linux - Software 1 01-28-2005 01:45 AM
gprof: gmon.out file is missing call-graph data hemk76 Programming 0 01-07-2005 11:54 PM
What do you call your remote control? I call mine... RHLinuxGUY General 19 10-18-2004 06:23 AM
java, call function in calling object exodist Programming 9 06-13-2004 11:49 PM
Event driven object-to-object: C++ template class mecanism ( NOT STL or STDC++) bretzeltux Programming 2 12-23-2003 02:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration