Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

NiBLiTz

Constructors and Destructors

This topic is 6705 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Ahh ok, thanks, you explained it clearly and i sort of understand it now, SO i think i''ll leave it alone

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
------ 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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!