Topic: virtual void destructor


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

Hi,
You can always have a virtual destructor, but moment an object of it's type or
it's derived type is instantiated, it needs to be defined. Mind , there is
difference between declaration and definition . "=0" is a virtual specifier
which can be used to decalre a pure virtual function. Later on u can always
provide the definition for that function. So any class declared with pure
virtual function will be always an abstract.

Cheers
Neelesh

Ray Rizzuto wrote:

> Hello,
>
> I'm hoping someone can explain what use a pure virtual destuctor would have.
> I.e.:
>
> class x
> {
>     virtual ~x() = 0;
> };
>
> Class x can't be instantiated, but I'd think that any class derived from x
> would have a similar problem.  I.e. if y is derived from x, y's destructor
> will need to call the base class's destructor, but the base class (x)
> doesn't have one.
>
> The reason I'm asking this is that I ran across this in code I was trying to
> compile on Solaris, and that compiler complained.  I check the 1997 draft of
> the C++ standard, and it did say in section 12.4 that a pure virtual
> destructor was allowed.  I asked a friend who has the final version of the
> standard to check if that was still the case, and he said it was.
>
> I just want to understand this idiom before I blindly modify the code that
> doesn't compile.  Maybe the code is right, and the compiler has a bug?
>
> Ray Rizzuto
> ---
> [ 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              ]

--
Neelesh Vaikhary
Sr. Consultant

Xpedior,
Email : Neelesh.Vaikhary@Xpedior.com,
Phone : 415-399-7288 (W), 510-885-9367(H), 415-577-NEEL(C).


--------------05BC1DD0DD9FED712C400657
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

--------------05BC1DD0DD9FED712C400657--
---
[ 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 <7uoh3n$758$1@tikehau.netreach.net> "Ray Rizzuto" <r.j.rizzuto@ieee.org> writes:
>If a pure virtual member function has an implementation, is the class still
>abstract?

Yes.

>I.e. if I have the following
>
>class x
>{
>public:
>  virtual ~x()= 0;
>  virtual void test()= 0;
>};
>
>class y
>{
>public:
>   void test() {cout << "hello";}
>}
>
>Is class x abstract

Yes.

>even if there are implementation of ~x and test in the .cpp file?

Yes.

>Is class y abstract because it doesn't implement a destructor
>because of the pure virtual destructor in x?

No, y is not abstract, it has nothing to do with x and has
no pure virtuals.  ??

- 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: Hyman Rosen <hymie@prolifics.com>
Date: 1999/10/24
Raw View
"Ray Rizzuto" <r.j.rizzuto@ieee.org> writes:
> If a pure virtual member function has an implementation, is the class still
> abstract?  I.e. if I have the following
> class x
> {
> public:
>   virtual ~x()= 0;
>   virtual void test()= 0;
> };
> class y
> {
> public:
>    void test() {cout << "hello";}
> }
>
> Is class x abstract even if there are implementation of ~x and test in the
> .cpp file?  Is class y abstract because it doesn't implement a destructor
> because of the pure virtual destructor in x?

Class x is abstract because it has methods declared with '= 0'.
whether or not those methods have implementations is irrelevant,
and in fact abstract destructors *must* have implementations.

Class y is not abstract. Every class has a defined destructor.
If you don't write it, the compiler writes it for you.
---
[ 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: David R Tribble <david@tribble.com>
Date: 1999/10/22
Raw View
Ray Rizzuto wrote:
>
> Hello,
>
> I'm hoping someone can explain what use a pure virtual destuctor would
> have.  I.e.:
>
>    class x
>    {
>        virtual ~x() = 0;
>    };
>
> Class x can't be instantiated, but I'd think that any class derived
> from x would have a similar problem.  I.e. if y is derived from x,
> y's destructor will need to call the base class's destructor, but
> the base class (x) doesn't have one.

Yes it does; you've provided a declaration for it in the class.

Making the dtor virtual means that derived classes can override it
and call it polymorphically.  Making it pure virtual just means that
you might or might not provide a definition for x::~x().

But the problem is that it's private, which probably explains why
you can't instantiate an object of type x or inherit class x;
both require that the class dtor be accessible (not private).

-- David R. Tribble, david@tribble.com, http://www.david.tribble.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: Neelesh Vaikhary <Neelesh.Vaikhary@Xpedior.com>
Date: 1999/10/22
Raw View
This is a multi-part message in MIME format.
--------------134E92124A54D8FA64670A27
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

As per the C++ standard (12.4) -
The destructor can be virtual or pure virtual, but it shall be defined if any
object of it or its derived type is created.
That pretty much answers your questions. I think previous compilers won't allow
this.
Neelesh

Ray Rizzuto wrote:

> Hello,
>
> I'm hoping someone can explain what use a pure virtual destuctor would have.
> I.e.:
>
> class x
> {
>     virtual ~x() = 0;
> };
>
> Class x can't be instantiated, but I'd think that any class derived from x
> would have a similar problem.  I.e. if y is derived from x, y's destructor
> will need to call the base class's destructor, but the base class (x)
> doesn't have one.
>
> The reason I'm asking this is that I ran across this in code I was trying to
> compile on Solaris, and that compiler complained.  I check the 1997 draft of
> the C++ standard, and it did say in section 12.4 that a pure virtual
> destructor was allowed.  I asked a friend who has the final version of the
> standard to check if that was still the case, and he said it was.
>
> I just want to understand this idiom before I blindly modify the code that
> doesn't compile.  Maybe the code is right, and the compiler has a bug?
>
> Ray Rizzuto
> ---
> [ 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              ]

-

--------------134E92124A54D8FA64670A27
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

--------------134E92124A54D8FA64670A27--
---
[ 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: "Ray Rizzuto" <r.j.rizzuto@ieee.org>
Date: 1999/10/22
Raw View
If a pure virtual member function has an implementation, is the class still
abstract?  I.e. if I have the following

class x
{
public:
  virtual ~x()= 0;
  virtual void test()= 0;
};

class y
{
public:
   void test() {cout << "hello";}
}

Is class x abstract even if there are implementation of ~x and test in the
.cpp file?  Is class y abstract because it doesn't implement a destructor
because of the pure virtual destructor in x?

Thanks,

Ray
Salters wrote in message <380EC2A2.F6446A38@lucent.com>...
>Ray Rizzuto wrote:
>
>> Hello,
>
>> I'm hoping someone can explain what use a pure virtual destuctor would
have.
>> I.e.:
>
>> class x
>> {
>>     virtual ~x() = 0;
>> };
>
>> Class x can't be instantiated, but I'd think that any class derived from
x
>> would have a similar problem.  I.e. if y is derived from x, y's
destructor
>> will need to call the base class's destructor, but the base class (x)
>> doesn't have one.
>
>> The reason I'm asking this is that I ran across this in code I was trying
to
>> compile on Solaris, and that compiler complained.  I check the 1997 draft
of
>> the C++ standard, and it did say in section 12.4 that a pure virtual
>> destructor was allowed.  I asked a friend who has the final version of
the
>> standard to check if that was still the case, and he said it was.
>
>> I just want to understand this idiom before I blindly modify the code
that
>> doesn't compile.  Maybe the code is right, and the compiler has a bug?
>
>> Ray Rizzuto
>
>It is legal to provide an implementation for a pure virtual function. This
>can only be reached by an explicit call.
>
>I.e.
>class C1 {
> virtual void f() =0;
>};
>class C2:public C1 {
> void f() {C1::f()};
>void C1::f() {};
>
>C2 c;
>c.f() calls C2::f, which calls C1::f() directly.
>
>The same mechanism applies to destructor, except for the fact that they
>automatically call their base counterpart.
>
>Michiel Salters
>---
>[ 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              ]
---
[ 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: clamage@eng.sun.com (Steve Clamage)
Date: 1999/10/22
Raw View
"Ray Rizzuto" <r.j.rizzuto@ieee.org> writes:

>I'm hoping someone can explain what use a pure virtual destuctor would have.
>I.e.:

>class x
>{
>    virtual ~x() = 0;
>};

>Class x can't be instantiated, but I'd think that any class derived from x
>would have a similar problem.  I.e. if y is derived from x, y's destructor
>will need to call the base class's destructor, but the base class (x)
>doesn't have one.

A destructor can be pure virtual, but you must provide a definition
(body) for it if any object derived from the type is destroyed.
Recall that you can supplpy a definition of any pure virtual
function, and if it has a definition it can be called. And the
destructor of any derived object always calls the destructor
for each of its direct base classes.

--
Steve Clamage, stephen.clamage@sun.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: "Ray Rizzuto" <r.j.rizzuto@ieee.org>
Date: 1999/10/21
Raw View
Hello,

I'm hoping someone can explain what use a pure virtual destuctor would have.
I.e.:

class x
{
    virtual ~x() = 0;
};

Class x can't be instantiated, but I'd think that any class derived from x
would have a similar problem.  I.e. if y is derived from x, y's destructor
will need to call the base class's destructor, but the base class (x)
doesn't have one.

The reason I'm asking this is that I ran across this in code I was trying to
compile on Solaris, and that compiler complained.  I check the 1997 draft of
the C++ standard, and it did say in section 12.4 that a pure virtual
destructor was allowed.  I asked a friend who has the final version of the
standard to check if that was still the case, and he said it was.

I just want to understand this idiom before I blindly modify the code that
doesn't compile.  Maybe the code is right, and the compiler has a bug?

Ray Rizzuto
---
[ 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: Salters <salters@lucent.com>
Date: 1999/10/21
Raw View
Ray Rizzuto wrote:

> Hello,

> I'm hoping someone can explain what use a pure virtual destuctor would have.
> I.e.:

> class x
> {
>     virtual ~x() = 0;
> };

> Class x can't be instantiated, but I'd think that any class derived from x
> would have a similar problem.  I.e. if y is derived from x, y's destructor
> will need to call the base class's destructor, but the base class (x)
> doesn't have one.

> The reason I'm asking this is that I ran across this in code I was trying to
> compile on Solaris, and that compiler complained.  I check the 1997 draft of
> the C++ standard, and it did say in section 12.4 that a pure virtual
> destructor was allowed.  I asked a friend who has the final version of the
> standard to check if that was still the case, and he said it was.

> I just want to understand this idiom before I blindly modify the code that
> doesn't compile.  Maybe the code is right, and the compiler has a bug?

> Ray Rizzuto

It is legal to provide an implementation for a pure virtual function. This
can only be reached by an explicit call.

I.e.
class C1 {
 virtual void f() =0;
};
class C2:public C1 {
 void f() {C1::f()};
void C1::f() {};

C2 c;
c.f() calls C2::f, which calls C1::f() directly.

The same mechanism applies to destructor, except for the fact that they
automatically call their base counterpart.

Michiel Salters
---
[ 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              ]