7 Replies - 759 Views - Last Post: 22 February 2009 - 02:38 PM Rate Topic: -----

#1 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Question about Constructors and Destructors

Posted 22 February 2009 - 02:15 PM

Do i need to call them, or are they called automatically.
Is This A Good Question/Topic? 0
  • +

Replies To: Question about Constructors and Destructors

#2 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: Question about Constructors and Destructors

Posted 22 February 2009 - 02:17 PM

As long as you do not specify a constructor and/or a destructor they are automatically defined.

This post has been edited by e_barroga: 22 February 2009 - 02:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 Sadaiy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 03-October 08

Re: Question about Constructors and Destructors

Posted 22 February 2009 - 02:22 PM

You don't need a destructor in C++ unless you have pointers. You need to delete the pointers in the destructor.

A constructor may be necessary to initialize the data members of an object.
Was This Post Helpful? 0
  • +
  • -

#4 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: Question about Constructors and Destructors

Posted 22 February 2009 - 02:25 PM

I thought the data members were automatically initialized to zero when the object is created.
Was This Post Helpful? 0
  • +
  • -

#5 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Question about Constructors and Destructors

Posted 22 February 2009 - 02:28 PM

They aren't initialized to zero, they are initialized with whatever random data the memory contained. If you have an unitialized integer you try to cout, you're likely to get -34572396 or something meaningless to the program and you. The default constructor C++ gives to classes without their own does almost nothing for you. If you want data, objects, or anything else initialized or done, you have to tell the computer how with your own constructor.

Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#6 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: Question about Constructors and Destructors

Posted 22 February 2009 - 02:29 PM

Data members are not initialized with a value.... in the same sense as regular variables are:

int my_variable;



Also note that you cannot initialize data members with a value the same time you declare them:

The following is illegal
class my_class {
  int my_member = 0;
};


Was This Post Helpful? 0
  • +
  • -

#7 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Question about Constructors and Destructors

Posted 22 February 2009 - 02:34 PM

Unless of course, the data members are static and const, only then can you initialize a variable in a class in its definition, because there is only one copy of it and it never changes. But only then can you initialize a variable in the actual class definition and not a function or constructor. :D
Was This Post Helpful? 0
  • +
  • -

#8 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Question about Constructors and Destructors

Posted 22 February 2009 - 02:38 PM

If you want your variables initialized to zero, you would have to write something like this:
class Example
{
   int someVar;
   int anotherVar;
   Example()
   {
      someVar = 0;
      anotherVar = 0;
   }
};


Also, you only need a destructor if you allocate memory off the heap and/or use pointers.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1