Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,413 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,433 people online right now. Registration is fast and FREE... Join Now!




Copy constructor and destructor

 
Reply to this topicStart new topic

Copy constructor and destructor, Simple question

DemonGal711
14 Oct, 2008 - 07:09 PM
Post #1

New D.I.C Head
*

Joined: 25 Sep, 2008
Posts: 11

Okay, I have a list and I need to test my functions, two of which are the copy constructor and the destructor. I was wondering if it is possible for me to make a separate function, make a list in it (let's call it list a), copy it to another list that returns to the main (list cool.gif, let the original be destroyed, and then trying to print list a from the main. Would that be a good way to show what a copy constructor does and what the destructor does?

If not, can anyone suggest some way of doing this?
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Copy Constructor And Destructor
14 Oct, 2008 - 09:03 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Well your post is a little hard to read but lets see if I understand:

You need to demonstrate the use of copy constructors and deconstructions.

To do this you have a class that is a list. You plan on creating a list A and then copying it to list B, and then destroying list A.

It is not always super easy to see the deconstructor execute. There is another thread that you may find interesting.


Generally I would say your idea is ok.
User is offlineProfile CardPM
+Quote Post

DemonGal711
RE: Copy Constructor And Destructor
14 Oct, 2008 - 09:31 PM
Post #3

New D.I.C Head
*

Joined: 25 Sep, 2008
Posts: 11

Yeah, that's basically what I'm trying to do. I know it's hard to see the deconstructor execute, but that is what we have to do so I'm trying to figure out a way to do it and that's the best I could come up with.
User is offlineProfile CardPM
+Quote Post

Psionics
RE: Copy Constructor And Destructor
14 Oct, 2008 - 11:52 PM
Post #4

D.I.C Head
Group Icon

Joined: 6 Sep, 2008
Posts: 122



Thanked: 2 times
Dream Kudos: 100
My Contributions
To give you a visual example of testing destructors (just so you know, it's not called a deCONstructor wink2.gif )and constructors, I found this code. I was working with Queues and this was the main I made to test the class:

cpp

int main(void)
{
// this should be zero since nothings been made yet
cout << "Current nodes allocated: " << CQueue::GetTotal() << "\n\n";

// Create a bunch of queues
CQueue q(25); // this should push 25
CQueue q2; // this should push 50
CQueue* pQ3 = new CQueue(100); // this should only push 50

// Add some info to the queue
cout << "Enqueueing:\n";
for(int i = 0; i < 50; ++i)
{
q.Enqueue(i);
q2.Enqueue(i);
pQ3->Enqueue(i);

cout << i << ' '; // show the queues
}

cout << "\n\n";

// this should give 125 if the constructor worked correctly
// 25 for q, 50 for q2, and 50 for q3
cout << "Current nodes allocated: " << q.GetTotal() << "\n\n";

delete pQ3; // run the destructor

// this should give 75 if the destructor worked
// 50 for q2, 25 for q
cout << "Current nodes allocated: " << CQueue::GetTotal() << "\n\n";

cout << "Dequeueing:\n";
int nTemp;
while(q.Dequeue(nTemp))
cout << nTemp << ' ';

cout << "\n\n";

// this was to test if my dequeue worked,
// yields 50
cout << "Current nodes allocated: " << CQueue::GetTotal() << "\n\n";

return 0; // No problemos!
}


If you want to test the copy constructor, you could just do something like this:
cpp

CClassName c;
CClassName a = c;

// bam! done haha just print off c


Hope this helps smile.gif

This post has been edited by Psionics: 14 Oct, 2008 - 11:58 PM
User is offlineProfile CardPM
+Quote Post

DemonGal711
RE: Copy Constructor And Destructor
15 Oct, 2008 - 07:43 AM
Post #5

New D.I.C Head
*

Joined: 25 Sep, 2008
Posts: 11

Okay, I got the copy constructor one, but I'm still not sure how to show that my destructor works. I can read the code and all, but I'm not sure how to implement it in my coding. I'm not sure if they want me to write a list and then delete it and show it won't output, or to do something else to it.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:39PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month