Advertisement

Constructors and Destructors

Started by April 12, 2000 07:27 AM
4 comments, last by NiBLiTz 24 years, 6 months ago
Please Explain??? I think you may have to go into detail abotu the way memory is used, cause i keep reading up that it is mainly there to free up memory used by that class when it is done... But anyways, could someone explain this for me. NiBLiTz - ALL that is needed
NiBLiTz - ALL that is needed
The Basics: constructors allow you to initialise a class automatically when you make it - destructors allow you to do any ''clearing up'' when an object is deleted/goes out of scope.

eg:
class TheTime
{
TheTime(); // default contructor
time_t value;
};

// This constructor initialises the value to be the current time.
TheTime::TheTime()
{
value = time(NULL);
}

Now, when you declare a TheTime object, you can guarantee that its ''value'' member will be initialised to the time of creation. Eg:
TheTime myTimeObject;
No further initialisation is needed.

Destructors are not so commonly used, as you rarely need to change values when an object is being deleted! But sometimes you allocate memory as part of a class, and therefore when you destroy an instance of that class, you want to deallocate the memory too.

Example:
class AllocSomeMem
{
AllocSomeMem(); // default constructor
~AllocSomeMem(); // destructor

char* buffer; // A pointer to our memory
}

AllocSomeMem::AllocSomeMem()
{
buffer = new char[1000]; // Allocate an array of 1000 chars
}

AllocSomeMem::~AllocSomeMem()
{
delete buffer; // If we have memory here, deallocate it
}

Now, when you make an instance of AllocSomeMem, it will automatically grab the 1000 char array and point to it with buffer, and when you destroy that object, it will automatically release that memory too.

That is the basic idea behind constructors and destructors. There are many more advanced topics though, such as initialisation lists, virtual destructors, copy constructors, conversion constructors, etc etc. So many that I recommend you get a C++ book to find out about them. (Or see what else gets posted here ) The 21 days, 24 hrs,and Dummies types of books all cover these in enough detail for the relative beginner, as far as I know.
Advertisement
Ahh ok, thanks, you explained it clearly and i sort of understand it now, SO i think i''ll leave it alone
NiBLiTz - ALL that is needed
quote: Original post by Kylotan

class AllocSomeMem
{
AllocSomeMem(); // default constructor
~AllocSomeMem(); // destructor

char* buffer; // A pointer to our memory
}

AllocSomeMem::AllocSomeMem()
{
buffer = new char[1000]; // Allocate an array of 1000 chars
}

AllocSomeMem::~AllocSomeMem()
{
delete buffer; // If we have memory here, deallocate it
}


shouldn''t that be delete buffer[]? If I remeber correctly, you need to do that in order to delete more than the first elemement. Plz correct me if I am wrong.

--------------------


You are not a real programmer until you end all your sentences with semicolons;

Yanroy@usa.com

Visit the ROAD Programming Website

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

------ Yaroy said ----------------------------
quote:
--------------------------------------------------------------------------------
Original post by Kylotan

class AllocSomeMem
{
AllocSomeMem(); // default constructor
~AllocSomeMem(); // destructor

char* buffer; // A pointer to our memory
}

AllocSomeMem::AllocSomeMem()
{
buffer = new char[1000]; // Allocate an array of 1000 chars
}

AllocSomeMem::~AllocSomeMem()
{
delete buffer; // If we have memory here, deallocate it
}



--------------------------------------------------------------------------------


shouldn''t that be delete buffer[]? If I remeber correctly, you need to do that in order to delete more than the first elemement. Plz correct me if I am wrong.

---------- END --------------------------

Indeed, you need delete[] buffer; to delete the whole array and not just the first one. That''s with VC++ though, not sure for BC++, I think the delete function does the same...




Cyberdrek
Headhunter Soft
DLC Multimedia
Two Guys Soft
[Cyberdrek | ]
Yep, you''re both right, lucky it was a ''concept'' post and not a ''fix the code'' post

This topic is closed to new replies.

Advertisement