![]() |
|
#1
|
|||
|
|||
Classes initiationWhat 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
|
|||
|
|||
Re: Classes initiationYou 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:
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
|
|||
|
|||
Re: Classes initiationQuote:
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:
...then a copy ctor is definitely called, not the assignment operator. :davis: |
|
#4
|
|||
|
|||
Re: Classes initiationQuote:
__________________
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 GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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