Topic: Pure Virtual Function


Author: "John D. Hickin" <hickin@Hydro.CAM.ORG>
Date: 1999/10/24
Raw View
Neelesh Vaikhary wrote:
>
> Hi,
> I read some where that a pure virtual fucntion can have a body, but when
> i read the C++ standard (10.4.2) it clearly says that a member function
> can have either a function body or pure specifier ( = 0) in it's
> declaration. So how is that possible?  Or is it like this - u can

The standard can be tough going in places but this one seems clear to
me. Section 10.4.2 states only that a virtual function is pure if the
function declaration in the class definition uses a _pure specifier_; a
reference to section 9.2 is made for the latter.

Even without reading 9.2 we have a hint: the use of the word declaration
as pertaining to function, rather than the use of the word definition
(which here would imply the presence a function body). Additionally, an
example is given that makes it clear that one cannot have a definition
and a pure specifier together.

As far as I can see, the closest one can come to what you have in mind
is:

  class X { inline virtual int f() = 0; }; int X::f() { return 42; }
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Paul Grealish" <paul.grealish@uk.geopak-tms.com>
Date: 1999/10/24
Raw View
Neelesh Vaikhary wrote in message <380FA3EB.D7A5A7F3@Xpedior.com>...
>Hi,
>I read some where that a pure virtual fucntion can have a body, but when
>i read the C++ standard (10.4.2) it clearly says that a member function
>can have either a function body or pure specifier ( = 0) in it's
>declaration. So how is that possible?  Or is it like this - u can
>declare it pure virtual but can define the body later somewhere?

Yes, your last statement is correct.


    struct Foo
    {
        virtual void f1(void) = 0;
    };
    void Foo::f1(void) {}  // Body of pure virtual

Some compilers (MSVC++ included) allow:

    virtual void f1(void) = 0 {};

although this isn't allowed by the standard.

+-------------------------------+
|         Paul Grealish         |
|    Bentley Transportation     |
|      Cambridge, England       |
|   paul.grealish@bentley.com   |
+-------------------------------+
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: comeau@panix.com (Greg Comeau)
Date: 1999/10/24
Raw View
In article <380FA3EB.D7A5A7F3@Xpedior.com> Neelesh Vaikhary <Neelesh.Vaikhary@Xpedior.com> writes:
>I read some where that a pure virtual fucntion can have a body, but when
>i read the C++ standard (10.4.2) it clearly says that a member function
>can have either a function body or pure specifier ( = 0) in it's
>declaration. So how is that possible?  Or is it like this - u can
>declare it pure virtual but can define the body later somewhere?

Yes, try it!

- Greg
--
       Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
     Producers of Comeau C/C++ 4.2.38 -- NOTE 4.2.42 BETAS NOW AVAILABLE
    Email: comeau@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
                *** WEB: http://www.comeaucomputing.com ***
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Ron" <nospam@this-address.org>
Date: 1999/10/24
Raw View
Neelesh Vaikhary <Neelesh.Vaikhary@Xpedior.com> wrote in message
news:380FA3EB.D7A5A7F3@Xpedior.com...
> Hi,
> I read some where that a pure virtual fucntion can have a body, but when
> i read the C++ standard (10.4.2) it clearly says that a member function
> can have either a function body or pure specifier ( = 0) in it's
> declaration. So how is that possible?  Or is it like this - u can
> declare it pure virtual but can define the body later somewhere?

Yes. Note that 10.4(2) also says:

    A pure virtual function need be defined only if explicitly called....

This clearly anticipates that you can define a pure virtual function. Being
as you can't define it in the declaration, you must therefore be able to
define it elsewhere. I suspect that the reason you can't define it in the
class declaration has to do with the unwieldy syntax (or maybe it introduces
a syntactic ambiguity?)

-- Ron
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Gene Bushuyev" <gbush@my-deja.com>
Date: 1999/10/24
Raw View
Neelesh Vaikhary <Neelesh.Vaikhary@Xpedior.com> wrote in message
news:380FA3EB.D7A5A7F3@Xpedior.com...
> Hi,
> I read some where that a pure virtual fucntion can have a body, but when
> i read the C++ standard (10.4.2) it clearly says that a member function
> can have either a function body or pure specifier ( = 0) in it's
> declaration. So how is that possible?  Or is it like this - u can
> declare it pure virtual but can define the body later somewhere?
>
Yes, 10.4p2 prohibits (with no good reason) a definition of the pure virtual
function in its declaration in its class: "a function declaration cannot
provide both a pure-specifier and a definition." The possibility of defining
a pure virtual function is granted in the same paragraph. You can provide a
definition of a pure virtual function outside of the class scope.

Gene Bushuyev.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "maxim" <maxk@touro.edu>
Date: 1999/10/25
Raw View
What the standard meant is that if a function has a pure virtual specifier
it does not REQUIRE a body. But it CAN have one. And this makes a lot of
sense in an inheritance heirarchy where some of the behavoir is commun to
all the subclasses and some is not.
For the meaning (usefullness) of it all refer to item 36 in "Effective C++"
by Scott Myers.
Good luck.
max

Neelesh Vaikhary wrote in message <380FA3EB.D7A5A7F3@Xpedior.com>...
>Hi,
>I read some where that a pure virtual fucntion can have a body, but when
>i read the C++ standard (10.4.2) it clearly says that a member function
>can have either a function body or pure specifier ( = 0) in it's
>declaration. So how is that possible?  Or is it like this - u can
>declare it pure virtual but can define the body later somewhere?
>
>Neelesh
>
>
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Ron Natalie <ron@sensor.com>
Date: 1999/10/25
Raw View
Neelesh Vaikhary wrote:
>
> Hi,
> I read some where that a pure virtual fucntion can have a body, but when
> i read the C++ standard (10.4.2) it clearly says that a member function
> can have either a function body or pure specifier ( = 0) in it's
> declaration. So how is that possible?  Or is it like this - u can
> declare it pure virtual but can define the body later somewhere?

Correct, the following is legal:

class A {
 virtual void foo() = 0;
};

void A::foo()  {
}

What doesn't get supported.  You just can't provide them in the same
place because the braces and the = 0 are syntactically confusing.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Neelesh Vaikhary <Neelesh.Vaikhary@Xpedior.com>
Date: 1999/10/22
Raw View
This is a multi-part message in MIME format.
--------------75B4B634C37C8E430C7FAF03
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hi,
I read some where that a pure virtual fucntion can have a body, but when
i read the C++ standard (10.4.2) it clearly says that a member function
can have either a function body or pure specifier ( = 0) in it's
declaration. So how is that possible?  Or is it like this - u can
declare it pure virtual but can define the body later somewhere?

Neelesh


--------------75B4B634C37C8E430C7FAF03
Content-Type: text/x-vcard; charset=us-ascii;
 name="Neelesh.Vaikhary.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Neelesh Vaikhary
Content-Disposition: attachment;
 filename="Neelesh.Vaikhary.vcf"

begin:vcard
n:Vaikhary;Neelesh
tel;cell:415-577-NEEL
tel;fax:415-399-7002
tel;home:510-885-9367
tel;work:415-399-7288
x-mozilla-html:TRUE
url:www.Xpedior.com
org:Xpedior
version:2.1
email;internet:Neelesh.Vaikhary@Xpedior.com
title:Sr. Consultant
adr;quoted-printable:;;44 Montgomery Street,=0D=0ASuite # 3200;San Francisco;CA;94104;USA
x-mozilla-cpt:;21168
fn:Neelesh Vaikhary
end:vcard

--------------75B4B634C37C8E430C7FAF03--
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]