Jump to content
  • Advertisement
Sign in to follow this  
Avont29

classes

This topic is 4845 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, i would probally prefer structors than classes, i really don't need anything to be private or public or whateer, nothing will change, but what exactly does a contructor and a decontrucot rdo, an example would be nice also :)

Share this post


Link to post
Share on other sites
Advertisement
For one think in C++ a struct is the same as a class with the only difference that all members are public.

A Constructor is way to initialize the values on your class/struct,
a destructor is mainly used, to free any by your class/struct allocated memory.

For example:


struct Test1
{
Test1()
: gc( NULL )
{
gc = new GraphicContext; // now you are sure that gc is initialized always, when you use Test1
}

~Test1()
{
if( gc ) delete gc; // only with this you can easily free all memory allocated by your class
}
GraphicContext* gc;
}


This is an really simple example, with not much purpose so far, but I hope you get the point.

Share this post


Link to post
Share on other sites
uhh, no, thats the most advanced structor i have ever seen in my life, could you please break that down, i didn't even know you could call a function inside a struct

Share this post


Link to post
Share on other sites
---
EDIT:
functions declared in a struct are not called, but function declarations.
normally you will split them as follows:

Header:
class Test1
{
// standard constructor
Test1();
}

Code-File:
Test1::Test1()
{
}

The same as with each other function you define in a header in declare in your code-file
---
As mentioned structs are the same as classes (in c++!):

a constructor is a member with the same name as your struct/class
a destructor is a member with also the same name as your struct/class and ~ as prefix...


The main difference is that in a class you have the chance to declare members private (only the class itself can access them, protected the class and all it's child can access them, or public everyone can access them).


each class/struct can have function members as you like:

lets take this a step further:

struct Test1
{
Test1()
: gc( NULL )
{
gc = new GraphicContext; // now you are sure that gc is initialized always, when you use Test1
}

~Test1()
{
if( gc ) delete gc; // only with this you can easily free all memory allocated by your class
}

void refresh()
{
graphiccontext_refresh( gc );
}
GraphicContext* gc;
}

Where graphiccontext_refresh is some routine from an third-party library

using Test1 would look than, as follows:

{
Test1 t1; // here the constructor will be called implicit as our contructor takes no parameters.

t1.refresh(); // call of Test1::refresh;
} // here the destructor for t1 will be called, as you leave the area, where t1 is declared.

EDIT:
NULL is just the same as 0, only that you use it to initialize pointers, which don't point on allocated memory.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sparhawk42
---
EDIT:
functions declared in a struct are not called, but function declarations.
normally you will split them as follows:

Header:
class Test1
{
// standard constructor
Test1();
}

Code-File:
Test1::Test1()
{
}

The same as with each other function you define in a header in declare in your code-file
---
As mentioned structs are the same as classes (in c++!):

a constructor is a member with the same name as your struct/class
a destructor is a member with also the same name as your struct/class and ~ as prefix...


The main difference is that in a class you have the chance to declare members private (only the class itself can access them, protected the class and all it's child can access them, or public everyone can access them).


each class/struct can have function members as you like:

lets take this a step further:

struct Test1
{
Test1()
: gc( NULL )
{
gc = new GraphicContext; // now you are sure that gc is initialized always, when you use Test1
}

~Test1()
{
if( gc ) delete gc; // only with this you can easily free all memory allocated by your class
}

void refresh()
{
graphiccontext_refresh( gc );
}
GraphicContext* gc;
}

Where graphiccontext_refresh is some routine from an third-party library

using Test1 would look than, as follows:

{
Test1 t1; // here the constructor will be called implicit as our contructor takes no parameters.

t1.refresh(); // call of Test1::refresh;
} // here the destructor for t1 will be called, as you leave the area, where t1 is declared.

EDIT:
NULL is just the same as 0, only that you use it to initialize pointers, which don't point on allocated memory.



could you explain the code :( you are "declaring" a function as a member, why?
and what is : gc(null) i don't know any of that stuff, does this have something t do with graphics? i dunno, idon't understand :(

Share this post


Link to post
Share on other sites
By the way I would suggest, that you start with a good tutorial on object oriented programming with C++, if you really want to see the whole picture.

Share this post


Link to post
Share on other sites
The point in declaring functions as members is to group code, like you do it with namespaces, the only difference is (and here come the classes into the game) that they can access all private members of your class and you can so garantue that all your private class members are always valid.

GraphicContext is just an class I made up for this example, nothing more.

A constructor works like follows:

Test1::Test1()
: gc( NULL ) // direct writing to the members, as soon as the memory is allocated
{
// doing some more complex stuff

gc = new GraphicContext; // in rare cases this might fail, if you don't have enough memory, for that purpose gc was initialized with NULL.
}


Share this post


Link to post
Share on other sites
Quote:
Original post by Sparhawk42
---
The main difference is that in a class you have the chance to declare members private.


Not true, a struct can declare members private if you want. The only, ONLY, difference between a class and a struct is the default access level for properties or methods. In a struct, the default level is public, and in a class, it is private. This is for backwards compatibility with regular C.


class Chicken
{
int someData;
};

struct Chicken // we are identical as above
{
private:
int someData;
};



In C++, structs can have methods just as classes do. They can also have constructors and destructors, just as classes do. A constructor is called upon the creation of an instance of the class, and the destructor is called when the instance dies.


class Chicken
{
public:
Chicken()
{
cout << "Hello World";
}

~Chicken()
{
cout << "We're dying.\n";
}
};



Thus calling "Chicken k" will immediately call Chicken(), and when k falls out of scope, it will call ~Chicken().

Hope that helps.

Share this post


Link to post
Share on other sites
oh, ok, i understand aobut 35% of that, i'll read up some more on classes, but i don't see why anyone would need any of their members private, nothing can change the members

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!