virtual functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rajesh kalra

    virtual functions

    do the virtual mechanism works when virtual functions are called in
    constructors?
  • Barry

    #2
    Re: virtual functions

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


    --
    Best Regards
    Barry

    Comment

    • Ian Collins

      #3
      Re: virtual functions

      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.

      Comment

      • Juha Nieminen

        #4
        Re: virtual functions

        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)?

        Comment

        • Juha Nieminen

          #5
          Re: virtual functions

          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).

          Comment

          • Barry

            #6
            Re: virtual functions

            Juha Nieminen wrote:
            Barry wrote:
            >rajesh kalra wrote:
            >>do the virtual mechanism works when virtual functions are called in
            >>constructor s?
            >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

            Comment

            • James Kanze

              #7
              Re: virtual functions

              On May 28, 10:41 am, Juha Nieminen <nos...@thanks. invalidwrote:
              Barry wrote:
              rajesh kalra wrote:
              do the virtual mechanism works when virtual functions are called in
              constructors?
              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:james.kan ze@gmail.com
              Conseils en informatique orientée objet/
              Beratung in objektorientier ter Datenverarbeitu ng
              9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

              Comment

              • ManicQin

                #8
                Re: virtual functions

                On May 28, 11:43 am, Juha Nieminen <nos...@thanks. invalidwrote:
                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

                Comment

                Working...