virtual functions

Discussion in 'C++' started by rajesh kalra, May 28, 2008.

  1. rajesh kalra

    rajesh kalra Guest

    do the virtual mechanism works when virtual functions are called in
    constructors?
     
    rajesh kalra, May 28, 2008
    #1
    1. Advertising

  2. rajesh kalra

    Barry Guest

    Barry, May 28, 2008
    #2
    1. Advertising

  3. rajesh kalra

    Ian Collins Guest

    rajesh kalra wrote:
    > do the virtual mechanism works when virtual functions are called in
    > constructors?


    Yes, but take care if you do!

    The function called is the one in the class under construction, or one
    of its base classes (12.7.3). If a function is pure virtual, it can't
    be called unless it has been overridden.

    --
    Ian Collins.
     
    Ian Collins, May 28, 2008
    #3
  4. Barry wrote:
    > rajesh kalra wrote:
    >> do the virtual mechanism works when virtual functions are called in
    >> constructors?

    >
    > Never do it!
    > http://www.artima.com/cppsource/nevercall.html


    Do Java and C# call the derived implementations of virtual functions
    even though the derived part has yet not been constructed (its
    constructor has not been called yet)?
     
    Juha Nieminen, May 28, 2008
    #4
  5. Ian Collins wrote:
    > rajesh kalra wrote:
    >> do the virtual mechanism works when virtual functions are called in
    >> constructors?

    >
    > Yes, but take care if you do!


    Curiously, it's even possible to call base class functions *before*
    the base class part of the object has been constructed at all, using a
    small trick (which even more curiously is legal C++, although of course
    extremely not recommended).
     
    Juha Nieminen, May 28, 2008
    #5
  6. rajesh kalra

    Barry Guest

    Juha Nieminen wrote:
    > Barry wrote:
    >> rajesh kalra wrote:
    >>> do the virtual mechanism works when virtual functions are called in
    >>> constructors?

    >> Never do it!
    >> http://www.artima.com/cppsource/nevercall.html

    >
    > Do Java and C# call the derived implementations of virtual functions
    > even though the derived part has yet not been constructed (its
    > constructor has not been called yet)?


    Yes, both


    --
    Best Regards
    Barry
     
    Barry, May 28, 2008
    #6
  7. rajesh kalra

    James Kanze Guest

    On May 28, 10:41 am, Juha Nieminen <> wrote:
    > Barry wrote:
    > > rajesh kalra wrote:
    > >> do the virtual mechanism works when virtual functions are called in
    > >> constructors?


    > > Never do it!
    > >http://www.artima.com/cppsource/nevercall.html


    > Do Java and C# call the derived implementations of virtual
    > functions even though the derived part has yet not been
    > constructed (its constructor has not been called yet)?


    Don't know about C#, but Java does. It can easily be a source
    of strange errors, too.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, May 28, 2008
    #7
  8. rajesh kalra

    ManicQin Guest

    On May 28, 11:43 am, Juha Nieminen <> wrote:
    > Ian Collins wrote:
    > > rajesh kalra wrote:
    > >> do the virtual mechanism works when virtual functions are called in
    > >> constructors?

    >
    > > Yes, but take care if you do!

    >
    > Curiously, it's even possible to call base class functions *before*
    > the base class part of the object has been constructed at all, using a
    > small trick (which even more curiously is legal C++, although of course
    > extremely not recommended).


    Can you post the trick (or refer to it)?
    Just for the fun of it.
    Thanks
     
    ManicQin, May 29, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Winter
    Replies:
    9
    Views:
    642
    Michael Winter
    Sep 24, 2003
  2. heted7
    Replies:
    33
    Views:
    1,075
    Chris Dearlove
    May 12, 2005
  3. Replies:
    3
    Views:
    394
    Nitin Motgi
    Jan 31, 2006
  4. Replies:
    7
    Views:
    393
    Richard
    Aug 28, 2006
  5. John Goche
    Replies:
    10
    Views:
    796
    Marcus Kwok
    Dec 8, 2006
Loading...

Share This Page