Re: Alternate syntax for pure virtual functions?

Discussion in 'C++' started by Maciej Sobczak, Aug 11, 2003.

  1. Nimmi Srivastav wrote:
    > According to C++ FAQs by Marshall P. Cline, Greg A. Lomow, Mike Girou,
    > FAQ21.11, a pure virtual function can be defined in the same class
    > that declares it. Unfortunately no coding example is provided because
    > "new C++ programmers don't usually understand what it means, so this
    > practice should be avoided".
    >
    > I am extremely curious how this is done. Could someone kindly provide
    > a code snippet?


    The problem is only with defining a pure virtual function *inside* a
    class (together with the function declaration), since the grammar does
    not allow it:

    // illegal (but MSVC++7.1 eats it and feels fine)
    class C
    {
    public:
    virtual void foo() = 0
    {
    }
    };

    However, there's no problem with this:

    // ok
    class C
    {
    public:
    virtual void foo() = 0;
    };

    void C::foo()
    {
    // ...
    }

    > "new C++ programmers don't usually understand what it means, so this
    > practice should be avoided".


    To be short:

    - you make function pure virtual when you think there *is no* reasonable
    implementation for it at the level of its class; it also means that
    derived classes are supposed to provide their own implementations
    - you define a function when you think there *is* reasonable
    implementation for it at the level of its class

    ;)

    If you've never seen it in a "real" code, it means it is usually not needed.

    --
    Maciej Sobczak
    http://www.maciejsobczak.com/

    Distributed programming lib for C, C++, Python & Tcl:
    http://www.maciejsobczak.com/prog/yami/


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Maciej Sobczak, Aug 11, 2003
    #1
    1. Advertising

  2. Maciej Sobczak

    Siemel Naran Guest

    "Maciej Sobczak" <> wrote in message

    > // illegal (but MSVC++7.1 eats it and feels fine)
    > class C
    > {
    > public:
    > virtual void foo() = 0
    > {
    > }
    > };


    Do you know why the standard prohibits you from defining a pure virtual
    function in the class definition?

    --
    +++++++++++
    Siemel Naran


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Siemel Naran, Aug 12, 2003
    #2
    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. David White
    Replies:
    0
    Views:
    433
    David White
    Aug 11, 2003
  2. Todd Aspeotis
    Replies:
    3
    Views:
    458
    Kanenas
    May 30, 2005
  3. Chris
    Replies:
    2
    Views:
    677
    Chris
    Mar 4, 2007
  4. John Goche
    Replies:
    10
    Views:
    736
    Marcus Kwok
    Dec 8, 2006
  5. Replies:
    10
    Views:
    336
    Sean O'Halpin
    Jul 17, 2006
Loading...

Share This Page