Monday, November 13, 2006

C++ : The Object Construction Process

When we define an object, such as

T object;

exactly what happens? If there is a constructor associated with T (either user supplied or synthesized by the compiler), it is invoked. That's obvious. What is sometimes less obvious is what the invocation of a constructor actually entails.

Constructors can contain a great deal of hidden program code because the compiler augments every constructor to a greater or lesser extent depending on the complexity of T's class hierarchy. The general sequence of compiler augmentations is as follows:

1. The data members initialized in the member initialization list have to be entered within the body of the constructor in the order of member declaration.

2. If a member class object is not present in the member initialization list but has an associated default constructor, that default constructor must be invoked.

3. Prior to that, if there is a virtual table pointer (or pointers) contained within the class object, it (they) must be initialized with the address of the appropriate virtual table(s).

4. Prior to that, all immediate base class constructors must be invoked in the order of base class declaration (the order within the member initialization list is not relevant).

o If the base class is listed within the member initialization list, the explicit arguments, if any, must be passed.

o If the base class is not listed within the member initialization list, the default constructor (or default memberwise copy constructor -- bitwise copy) must be invoked, if present.

o If the base class is a second or subsequent base class, the this pointer must be adjusted.

5. Prior to that, all virtual base class constructors must be invoked in a left-to-right, depth-first search of the inheritance hierarchy defined by the derived class.

o If the class is listed within the member initialization list, the explicit arguments, if any, must be passed. Otherwise, if there is a default constructor associated with the class, it must be invoked.

o In addition, the offset of each virtual base class subobject within the class must somehow be made accessible at runtime.

o These constructors, however, may be invoked if, and only if, the class object represents the "most-derived class." Some mechanism supporting this must be put into place.


Please click on the ads to your right :p

5 comments:

Anonymous said...

Dear Author zamanbakshifirst.blogspot.com !
I consider, that you commit an error. Let's discuss.

Zaman Bakshi said...

Unless you meant a typo, no, I didn't. Read 'Inside the C++ Object Model' -- Stanley Lippman.

Anonymous said...

Hello I'd love to thank you for such a great made forum!
I was sure this would be a nice way to make my first post!

Sincerely,
Edwyn Sammy
if you're ever bored check out my site!
[url=http://www.partyopedia.com/articles/football-party-supplies.html]football Party Supplies[/url].

Unknown said...

I'm having trouble in C++, thanks a lot!

heavy equipment sales

Anonymous said...

hi, good site very much appreciatted