Re: Pure virtual methods

Discussion in 'C++' started by Victor Bazarov, Jul 12, 2003.

  1. "Anders" <anders> wrote...
    > I know that when you create a pure virtual method in a class, you make it

    an
    > ABC. Is there anything else to note about pure virtual functions?


    They can (and should in some cases) have an implementation.

    Victor
     
    Victor Bazarov, Jul 12, 2003
    #1
    1. Advertising

  2. Victor Bazarov

    MiniDisc_2k2 Guest

    "Victor Bazarov" <> wrote in message
    news:Ij_Pa.46029$ye4.35615@sccrnsc01...
    > "Anders" <anders> wrote...
    > > I know that when you create a pure virtual method in a class, you make

    it
    > an
    > > ABC. Is there anything else to note about pure virtual functions?

    >
    > They can (and should in some cases) have an implementation.
    >
    > Victor
    >
    >


    Also (I'm not sure if this is standard), if you actually do end up calling
    the pure virtual function, you can provide an implementation for an
    undefined function (perhaps an error message to display or something).
    Define the function

    __purecall()
    {
    // stuff here
    }

    (Forgot return type, perhaps void?)

    --
    MiniDisc_2k2
     
    MiniDisc_2k2, Jul 13, 2003
    #2
    1. Advertising

  3. On Sun, 13 Jul 2003 17:18:50 +1000, "Chris \( Val \)" <> wrote:

    >
    >"Victor Bazarov" <> wrote in message
    >news:Ij_Pa.46029$ye4.35615@sccrnsc01...
    >| "Anders" <anders> wrote...
    >| > I know that when you create a pure virtual method in a class, you make it
    >| an
    >| > ABC. Is there anything else to note about pure virtual functions?
    >|
    >| They can (and should in some cases) have an implementation.
    >
    >In addition, those implementation(s), should not be
    >implemented in-line.
    >


    Why?
     
    Alf P. Steinbach, Jul 13, 2003
    #3
  4. "Alf P. Steinbach" <> wrote in message
    news:...
    | On Sun, 13 Jul 2003 17:18:50 +1000, "Chris \( Val \)" <> wrote:
    |
    | >
    | >"Victor Bazarov" <> wrote in message
    | >news:Ij_Pa.46029$ye4.35615@sccrnsc01...
    | >| "Anders" <anders> wrote...
    | >| > I know that when you create a pure virtual method in a class, you make it
    | >| an
    | >| > ABC. Is there anything else to note about pure virtual functions?
    | >|
    | >| They can (and should in some cases) have an implementation.
    | >
    | >In addition, those implementation(s), should not be
    | >implemented in-line.
    | >
    |
    | Why?

    Because it is illegal as defined by the standard.

    [10.4] - Abstract Classes - scroll down to the example...

    <QUOTE>
    Note: a function declaration cannot provide both a
    pure-specifier and a definition —end note]
    </QUOTE>

    Cheers.
    Chris Val
     
    Chris \( Val \), Jul 13, 2003
    #4
  5. "Andrey Tarasevich" <> wrote in message
    news:...
    | Chris ( Val ) wrote:
    | > | "Anders" <anders> wrote...
    | > | > I know that when you create a pure virtual method in a class, you make it
    | > | an
    | > | > ABC. Is there anything else to note about pure virtual functions?
    | > |
    | > | They can (and should in some cases) have an implementation.
    | >
    | > To OP:
    | >
    | > In addition, those implementation(s), should not be
    | > implemented in-line.
    | > ...
    |
    | As far as I understand, what you really meant to say is that pure
    | virtual functions shall not be defined within definition of its class.
    | As long as the definition of such function is located outside of the
    | class definition, it can be declared 'inline'. There is nothing illegal
    | about it.

    Yes, that is exactly what I tried to say :).

    Sorry if I wasn't too clear.

    Cheers.
    Chris Val
     
    Chris \( Val \), Jul 13, 2003
    #5
  6. On Sun, 13 Jul 2003 18:18:00 +1000, "Chris \( Val \)" <> wrote:

    >
    >"Alf P. Steinbach" <> wrote in message
    >news:...
    >| On Sun, 13 Jul 2003 17:18:50 +1000, "Chris \( Val \)" <> wrote:
    >|
    >| >
    >| >"Victor Bazarov" <> wrote in message
    >| >news:Ij_Pa.46029$ye4.35615@sccrnsc01...
    >| >| "Anders" <anders> wrote...
    >| >| > I know that when you create a pure virtual method in a class, you make it
    >| >| an
    >| >| > ABC. Is there anything else to note about pure virtual functions?
    >| >|
    >| >| They can (and should in some cases) have an implementation.
    >| >
    >| >In addition, those implementation(s), should not be
    >| >implemented in-line.
    >| >
    >|
    >| Why?
    >
    >Because it is illegal as defined by the standard.
    >
    >[10.4] - Abstract Classes - scroll down to the example...
    >
    ><QUOTE>
    >Note: a function declaration cannot provide both a
    > pure-specifier and a definition —end note]
    ></QUOTE>


    For completeness: Andrey Tarasevich has already explained why
    that isn't relevant -- it's just a specific way of inlining.

    Cheers,

    - lfA
     
    Alf P. Steinbach, Jul 13, 2003
    #6
  7. "Alf P. Steinbach" <> wrote in message
    news:...
    | On Sun, 13 Jul 2003 18:18:00 +1000, "Chris \( Val \)" <> wrote:
    |
    | >
    | >"Alf P. Steinbach" <> wrote in message
    | >news:...
    | >| On Sun, 13 Jul 2003 17:18:50 +1000, "Chris \( Val \)" <>
    wrote:
    | >|
    | >| >
    | >| >"Victor Bazarov" <> wrote in message
    | >| >news:Ij_Pa.46029$ye4.35615@sccrnsc01...
    | >| >| "Anders" <anders> wrote...
    | >| >| > I know that when you create a pure virtual method in a class, you make it
    | >| >| an
    | >| >| > ABC. Is there anything else to note about pure virtual functions?
    | >| >|
    | >| >| They can (and should in some cases) have an implementation.
    | >| >
    | >| >In addition, those implementation(s), should not be
    | >| >implemented in-line.
    | >| >
    | >|
    | >| Why?
    | >
    | >Because it is illegal as defined by the standard.
    | >
    | >[10.4] - Abstract Classes - scroll down to the example...
    | >
    | ><QUOTE>
    | >Note: a function declaration cannot provide both a
    | > pure-specifier and a definition —end note]
    | ></QUOTE>
    |
    | For completeness: Andrey Tarasevich has already explained why
    | that isn't relevant -- it's just a specific way of inlining.

    I think he(Andrey), agreed that it *is* relevant(defining the body
    of a pure virtual function outside of its class, which is *correct*
    according to the standard).

    Where the confusion seems to be, is in the terminology I used, and
    I have agreed with his correction of my terminology.

    However, my description was not that wrong(I believe), because
    defining the body of a function inside of a class is 'also' known
    as 'in-lined', hence my description.

    To be more accurate, what I really should have said is:

    "In addition, the implementation(s) for pure virtual functions,
    should not be implemented inside the definition of the class,
    but rather outside, as specified by the C++ standard."

    Cheers.
    Chris Val
     
    Chris \( Val \), Jul 13, 2003
    #7
  8. On Sun, 13 Jul 2003 21:30:41 -0400, "MiniDisc_2k2" <> wrote:

    >
    >"Alf P. Steinbach" <> wrote in message
    >news:...
    >> On Sun, 13 Jul 2003 17:18:50 +1000, "Chris \( Val \)"

    ><> wrote:
    >>
    >> >
    >> >"Victor Bazarov" <> wrote in message
    >> >news:Ij_Pa.46029$ye4.35615@sccrnsc01...
    >> >| "Anders" <anders> wrote...
    >> >| > I know that when you create a pure virtual method in a class, you

    >make it
    >> >| an
    >> >| > ABC. Is there anything else to note about pure virtual functions?
    >> >|
    >> >| They can (and should in some cases) have an implementation.
    >> >
    >> >In addition, those implementation(s), should not be
    >> >implemented in-line.
    >> >

    >>
    >> Why?
    >>

    >
    >How could you? Would it look something like this?
    >
    >class PureClass
    >{
    >public:
    > void Fxn() = 0 { cout << "oops?"; };
    >}


    No.


    >The point is that you cannot possibly code it in there. Something's just not
    >going to look right.... That's just not how it's done.


    Right.

    Move the definition outside the class. Remember the keyword
    'inline'.
     
    Alf P. Steinbach, Jul 14, 2003
    #8
  9. Victor Bazarov

    Ron Natalie Guest

    "Chris ( Val )" <> wrote in message news:ber4jl$7o7g2$-berlin.de...

    >
    > Because it is illegal as defined by the standard.
    >
    > [10.4] - Abstract Classes - scroll down to the example...
    >
    > <QUOTE>
    > Note: a function declaration cannot provide both a
    > pure-specifier and a definition —end note]
    > </QUOTE>
    >

    That's only one of two ways to define the function.
    You can put the definition outside the class and still have it
    inline.
     
    Ron Natalie, Jul 14, 2003
    #9
    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. qazmlp
    Replies:
    4
    Views:
    433
    David Harmon
    Feb 15, 2004
  2. IK
    Replies:
    2
    Views:
    612
    hemraj
    Jul 23, 2004
  3. John Goche
    Replies:
    10
    Views:
    758
    Marcus Kwok
    Dec 8, 2006
  4. Replies:
    7
    Views:
    591
    James Kanze
    May 2, 2007
  5. a
    Replies:
    7
    Views:
    365
    dasjotre
    Jun 28, 2007
Loading...

Share This Page