GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Jun-2006, 07:11
kdsXchris kdsXchris is offline
New Member
 
Join Date: May 2006
Posts: 12
kdsXchris is on a distinguished road

Classes initiation


What exactly happens when a new class is constructed? I read in my java cert book exactly how and what goes down when a new class is constructed, and it went something similar to the follow:

1) The super class constructor is called (each super super constructor is called and so on and so on...).
2) Each variable is given its default value, 0 or null...
3) The rest of the constructor statements execute.

So far, what I can deduce in C++ is the following:

1) Super classes are called, in the case of multiple inheritance, the class that comes first in the part where parent classes are listed goes first.
2) Variables are NOT given default values, only what was left over from memory...?
3) The rest of the constructor statements execute.

Is this pretty much what goes on, or does anything else happen?
  #2  
Old 04-Jun-2006, 04:35
MichaelS-R MichaelS-R is offline
Junior Member
 
Join Date: Apr 2006
Location: Berkshire, UK
Posts: 65
MichaelS-R is on a distinguished road

Re: Classes initiation


You are pretty much on the mark. However, you can specify default values if you want to (item 2) in the constructor and for static member variables in the declaration.

You should also watch out for when constructors (and particularly destructors are called). e.g. When assigning a class variable to another variable of the same class:
Code:
class A john, mary; ... mary=john; //No consutructor is called
This can cause confusion when, for example, a class is passed to a function by value, because the destructor will be called for each instance of the class.
Additionally, you have to be careful when deleting arrays of classes. If you miss out the [] you will not trigger all of the destructors (you may or may not want to but you do need to know).
Lots of this stuff in past threads on this board.
__________________
Michael

Dual Opteron 280 (2 x dual core) with 2Gb RAM, 2x36GB system drives, 2T on 3Ware 9500Mi RAID controller. Running Fedora Core 4. Using Anjuta IDE. Developemnt in C++ with MySQL (via mysql++).
  #3  
Old 04-Jun-2006, 08:05
davis
 
Posts: n/a

Re: Classes initiation


Quote:
Originally Posted by MichaelS-R
Code:
mary=john; //No consutructor is called

No ctor is called because the code above uses the assignment operator. The ctor is called in the declaration of variables of type A that are named mary and john. In other words, the ctor was called before the assignment. However, if the code were as follows:

Code:
class A john; class A mary = john; // cctor called

...then a copy ctor is definitely called, not the assignment operator.


:davis:
  #4  
Old 05-Jun-2006, 03:07
MichaelS-R MichaelS-R is offline
Junior Member
 
Join Date: Apr 2006
Location: Berkshire, UK
Posts: 65
MichaelS-R is on a distinguished road

Re: Classes initiation


Quote:
Originally Posted by davis
Code:
class A john; class A mary = john; // cctor called

...then a copy ctor is definitely called, not the assignment operator.
Thanks for the correction - obviously suffering from total brain fade. However, the point I was trying to make is that when the default copy constructor is called it is not the same as the normal class constructor and simply copies the memory occupied by that instance of the variable byte by byte. However, when the copy goes out of scope the normal class destructor is called! If you are creating anything which updates static member variables or releases memory (linked lists for example) this is not a good situation. So you must consider overloading the copy constructor.
__________________
Michael

Dual Opteron 280 (2 x dual core) with 2Gb RAM, 2x36GB system drives, 2T on 3Ware 9500Mi RAID controller. Running Fedora Core 4. Using Anjuta IDE. Developemnt in C++ with MySQL (via mysql++).
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Assistance with classes... Bravebird C++ Forum 7 27-Apr-2005 13:17
Help using const arrays in c++ classes FlipNode C++ Forum 5 31-Mar-2005 01:16
Fairly simple classes help please sammacs C++ Forum 0 30-Nov-2004 09:58
Using map<> with classes crystalattice C++ Forum 3 14-Nov-2004 16:23
First time using classes crystalattice C++ Forum 6 13-Oct-2004 08:21

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:46.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.