Jump to content
  • Advertisement
Sign in to follow this  
Avont29

constructors/deconstructors

This topic is 4816 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

hey, can someone please explain to me what a contructor/deconstructor is in a class, i've read the tuts, and they don't make sense, and im just not getting it, could you explain to me in the simplest way you can what a constructor/deconstructor is, thanks -Avont

Share this post


Link to post
Share on other sites
Advertisement
You create a class so that you can instantiate that class in the form of an object. For example:

// define MyClass class
class MyClass
{
...
};

// create a MyClass object
MyClass myobj;

Constructors are called when you instantiate it. What do you want to do when your class is constructed? Put them all down inside constructor. Destructors are called when the object is destroyed. What do you want to do when your class is destroyed? Put them all down inside destructor.

Share this post


Link to post
Share on other sites
I'm just starting to learn classes myself but maybe this will help

class Point {

public:
Point( float = 1.0, float = 1.0 ); // constructor with default paramaters
~Point();

private:
float x;
float y;

};

Point::Point( float X, float Y )
{
x = X;
y = Y;
}



now in main we can say:
Point p1; // this creates a Point with default x = 1.0 and y = 1.0
Point p2( 5.0, 3.0 ); // this creates a Point with x = 5.0 and y = 3.0

That's just a small example of something you can do with a constructor. I haven't got very deep into destructors yet so I don't have any good examples for those. :D

Hope that helps.

Share this post


Link to post
Share on other sites
A class is "nothing more" than a collection of related functions and data that are affected by the function calls. An object is an instance of a class.
You have an idea in your head of what an orange is. That's a class. You have an orange in your hand. That's an object.

A constructor is used basically to initialize the data inside the newly formed object. Otherwise your object could have some very wonky data inside that could corrupt your program. A destructor is used almost exclusively when you allocate memory while the program is running (dynamically).

Share this post


Link to post
Share on other sites
constructor/desctuctor(also refered to as ctor/dtor) are special 'functions' that get called on creation and deletion of an object.
these have the same names as the object, with the exception that the dtor has a tilde(~) 'prefix'.


class Object {
public:
// The default constructor, the complier generates this one if not present.
Object() : raw(0) {
}

Object(size_t size) : raw(0) {
this->raw = new int[size];
}

~Object() {
delete[] raw;
}

private:
int* raw;
};

{
// In this case, the default constructor(Object()) defined above is called.
Object obj;
}
// Out of scope, the destructor is called.

{
// Object(size_t size).
Object obj(50);
}
// Out of scope, destuctor is called, memory is deallocated.


Share this post


Link to post
Share on other sites
oh, ok, why would you need to make anything public or private, could u give me an example, and this jsut seems like a struct, but with public and private features

Share this post


Link to post
Share on other sites
Quote:
Original post by Avont29
oh, ok, why would you need to make anything public or private, could u give me an example, and this jsut seems like a struct, but with public and private features

A class is a struct, with private as the default access (struct is public by default). They are for security purposes, preventing unnecessary or unwanted changes that may break that class. For example, you can only operate a watch through the buttons that it provides for you. If you try to crack that watch open and modify it from the inside, chances are you are going to break that watch. The same thing as private/protected, they protect valuable information and operation that shouldn't be touched from outside.

Share this post


Link to post
Share on other sites
making stuff private is very useful as your projects tend to get bigger and more complex. However as a beginner you can pretty much ignore it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!