virtual functions and dynamic binding

Discussion in 'C++' started by mescaline, Dec 28, 2003.

  1. mescaline

    mescaline Guest

    If a function is declared a virtual function, in what cases will
    (some) compilers still do static binding?

    thanks
    mescaline, Dec 28, 2003
    #1
    1. Advertising

  2. "mescaline" <> wrote...
    > If a function is declared a virtual function, in what cases will
    > (some) compilers still do static binding?


    All compilers bind statically if a virtual function is called
    from a constructor or a destructor, or if it's called using
    fully qualified name (IIRC).

    Victor
    Victor Bazarov, Dec 29, 2003
    #2
    1. Advertising

  3. mescaline

    Ron Natalie Guest

    "Victor Bazarov" <> wrote in message news:fLKHb.108$xX.800@attbi_s02...
    \
    >
    > All compilers bind statically if a virtual function is called
    > from a constructor or a destructor,


    Only if called directly from the constructor or destructor. However,
    virtual functions are not disabled during construction, the only thing that
    changes is that the effective type of the object is the class that the constructor
    /destructor is being executed for.

    struct B {
    virtual void v();
    void nv() { v(); }
    };

    struct D : B {
    void v();
    D() { nv(); } // results in D::v() getting called.
    Ron Natalie, Dec 29, 2003
    #3
  4. "mescaline" <> wrote in message
    news:...
    > If a function is declared a virtual function, in what cases will
    > (some) compilers still do static binding?
    >


    When they know what the actual function must be - e.g ctor,dtor or just

    X x;
    x.aVirtualFunction();

    > thanks
    Nick Hounsome, Dec 29, 2003
    #4
    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. Jordan
    Replies:
    2
    Views:
    2,518
    Jordan
    Feb 10, 2004
  2. Michael Winter
    Replies:
    9
    Views:
    607
    Michael Winter
    Sep 24, 2003
  3. Replies:
    3
    Views:
    365
    Nitin Motgi
    Jan 31, 2006
  4. Mike Stevenson

    virtual functions and dynamic binding

    Mike Stevenson, Jun 22, 2006, in forum: C++
    Replies:
    2
    Views:
    329
    Jim Langston
    Jun 23, 2006
  5. John Goche
    Replies:
    10
    Views:
    723
    Marcus Kwok
    Dec 8, 2006
Loading...

Share This Page