🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Laurent-David Hasson Sat, 20 Apr 1996 03:52
Class instanciation and virtual stuff..
#2975
Author: Laurent-David Hasson
Date: Sat, 20 Apr 1996 03:52
59 lines
3768 bytes
  I have a question about how objects are constructed, and when "virtual" 
services become available.

  I remember reading in the ARM that one should not call "virtual" functions 
from a constructor. The reason being that an object should not be considered 
fully initialized until the last constructor had ended.

  However, by mistake, in one of my program, i was calling a library function
which is calling virtual functions. I never noticed the error because
apparently, the compiler i was using at the time allowed this behavior to
work properly. However, i recently got a new version of the compiler, and
everything broke. It took me several days to realize what was happening.

  I am now left with doubts with respect to the availibility of "virtual"
services, and i am now wondering why it is the way it is, that is, why a
class type should not be known, and all the plumbing in place, before the
first constructor is called. Naively, i would assume that instanciating an
object would go as follows:
      - Allocate the memory for the whole class
      - Create the suporting structural stuff (like typeID, VT etc...)
      - Then call the constructor, which follows with the Yo-Yo construction
       to initialize all the data members and whatever.
 
  This makes sense to me because the type of the class would actualy be known
in the constructor of the base class. Instead, calling "typeid" on "this" in
the base constructor gets the base class' typename. I understand why one
should not lightly call a virtual function because the local vars would not
have been initialized yet. But what if i called a virtual function that did
initialize vars it needed (never access a var of the class before it has been
written to first)? Or a virtual function that does not use the local
members?  Why should that fail?

 This leads me to think that all the "virtual" things are actually taken care
 of AFTER all construction has been made. And i do not understand why this
is. Are there important penalties i do not understand, or pitfalls i do not
see?



Thanks,
Laurent.


PS: CC by email is much appreciated.

============================================================================
Microsoft Network, or any service owned in full or in part by Microsoft, is
prohibited from redistributing this work in any form, in whole or in part.
Copyright, Laurent Hasson, 1996. License to distribute this post is
available to Microsoft for US$10,000 per instance. Posting without 
permission constitutes an agreement to these terms. Please send notices of 
violation to ldh@interlog.com and postmaster@microsoft.com
============================================================================
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads