c++ class ~

Hi All

I just created a class and I get two voids in the body of the class. One is called CGMI and ~CGMI.

My questions is what are these 2 void that were automatically generated? I mean they are voids that have the same name
as the Class its self so why would this appear? and what does the "~" mean?

1
2
3
4
5
6
class CGMI
{
    public:
    CGMI(void);//What is this?
    ~CGMI(void);//And what is this? Especially what does the "~" denote?
};


Also why would it have a void between the parenthsis? I mean i would except only parameters to be inside these parenthsis i.e.

 
void this_is_a_function(string enter_your_name){}//etc 


Many thanks for your help.
Last edited on
The first is the constructor, the second is the destructor.
A single void in the parameter list indicates that a function takes no parameters. It's not necessary, just a relict from C.
A constructor is used to initialize an object of the class. Generally this means assigning starting values to its member variables.

A destructor is called (automatically) when the object is destroyed. In most cases you can leave this one empty, but if you're using dynamic memory (i.e. used the "new" keyword in the constructor), you'll need to delete it there.
Many thanks guys. Thats a great help.
Last edited on
Topic archived. No new replies allowed.