Topic: Virtual methods like virtual destructors
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/10/04 Raw View
My apologies for reposting this. I had some trouble with my newsreader,
and parts of the my previous version were confusingly marked as if they
were quoted, even though they were actually new material.
Alex Vinokur wrote:
>
> In article <7t10cn$m9s$1@engnews1.eng.sun.com>,
> clamage@eng.sun.com (Steve Clamage) wrote:
> > Alex Vinokur <alexander.vinokur@telrad.co.il> writes:
> [snip]
> >
> > >Is it worth adding to C++ new kind of virtual methods
> > > (for instance, hard-virtual)
> > > that behave like virtual destructors?
> >
> > What behavior did you have in mind? Automatically call the
> > base-class function they override? Or something else?
> >
> [snip]
>
> I would like to get the following results.
>
> Alex
>
> //#########################################################
> //------------------- Wanted C++ code : BEGIN -------------
>
> #include <iostream>
>
> #define IT_IS_ME cout << __PRETTY_FUNCTION__ << endl;
> // __PRETTY_FUNCTION__ is predefined variable in the GNU compiler
>
> class BBB
> {
> public :
> virtual ~BBB () { IT_IS_ME; }
> virtual void foo1 () { IT_IS_ME; }
>
> //----- New kinds of virtuality ----------
> pre-virtual void foo2 () { IT_IS_ME; }
> post-virtual void foo3 () { IT_IS_ME; }
> //----------------------------------------
> };
>
> class DDD : public BBB
> {
> public :
> ~DDD () { IT_IS_ME; }
> void foo1 () { IT_IS_ME; }
> void foo2 () { IT_IS_ME; }
> void foo3 () { IT_IS_ME; }
> };
>
> int main ()
> {
> DDD ddd;
> cout << "\tOrdinary virtual method" << endl;
> ddd.foo1 ();
> cout << endl;
>
> cout << "\tpre-virtual method" << endl;
> ddd.foo2 ();
> cout << endl;
>
> cout << "\tpost-virtual method" << endl;
> ddd.foo3 ();
> cout << endl;
>
> cout << "\ttOrdinary virtual destructor" << endl;
>
> return 0;
> }
>
> //------------------- Wanted C++ code : END ---------------
>
> //#########################################################
> //------------------- Wanted Running Results : BEGIN ------
>
> Ordinary virtual method
> void DDD::foo1()
>
> pre-virtual method
> void BBB::foo2()
> void DDD::foo2()
>
> post-virtual method
> void DDD::foo3()
> void BBB::foo3()
>
> Ordinary virtual destructor
> DDD::~DDD()
> BBB::~BBB()
>
> //------------------- Wanted Running Results : END --------
You don't need a new language feature to achieve that result. The
following is only a little more complicated, and a lot more flexible:
> class BBB
> {
> public :
> virtual ~BBB () { IT_IS_ME; }
> virtual void foo1 () { IT_IS_ME; }
>
virtual void foo2_v();
virtual void foo3_v();
void foo2 () { IT_IS_ME; foo2_v(); }
void foo3 () { foo3_v(); IT_IS_ME; }
> //----------------------------------------
> };
>
> class DDD : public BBB
> {
> public :
> ~DDD () { IT_IS_ME; }
> void foo1 () { IT_IS_ME; }
void foo2_v () { IT_IS_ME; }
void foo3_v () { IT_IS_ME; }
> };
---
[ 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: Alex Vinokur <alexander.vinokur@telrad.co.il>
Date: 1999/10/03 Raw View
In article <7t10cn$m9s$1@engnews1.eng.sun.com>,
clamage@eng.sun.com (Steve Clamage) wrote:
> Alex Vinokur <alexander.vinokur@telrad.co.il> writes:
[snip]
>
> >Is it worth adding to C++ new kind of virtual methods
> > (for instance, hard-virtual)
> > that behave like virtual destructors?
>
> What behavior did you have in mind? Automatically call the
> base-class function they override? Or something else?
>
[snip]
I would like to get the following results.
Alex
//#########################################################
//------------------- Wanted C++ code : BEGIN -------------
#include <iostream>
#define IT_IS_ME cout << __PRETTY_FUNCTION__ << endl;
// __PRETTY_FUNCTION__ is predefined variable in the GNU compiler
class BBB
{
public :
virtual ~BBB () { IT_IS_ME; }
virtual void foo1 () { IT_IS_ME; }
//----- New kinds of virtuality ----------
pre-virtual void foo2 () { IT_IS_ME; }
post-virtual void foo3 () { IT_IS_ME; }
//----------------------------------------
};
class DDD : public BBB
{
public :
~DDD () { IT_IS_ME; }
void foo1 () { IT_IS_ME; }
void foo2 () { IT_IS_ME; }
void foo3 () { IT_IS_ME; }
};
int main ()
{
DDD ddd;
cout << "\tOrdinary virtual method" << endl;
ddd.foo1 ();
cout << endl;
cout << "\tpre-virtual method" << endl;
ddd.foo2 ();
cout << endl;
cout << "\tpost-virtual method" << endl;
ddd.foo3 ();
cout << endl;
cout << "\ttOrdinary virtual destructor" << endl;
return 0;
}
//------------------- Wanted C++ code : END ---------------
//#########################################################
//------------------- Wanted Running Results : BEGIN ------
Ordinary virtual method
void DDD::foo1()
pre-virtual method
void BBB::foo2()
void DDD::foo2()
post-virtual method
void DDD::foo3()
void BBB::foo3()
Ordinary virtual destructor
DDD::~DDD()
BBB::~BBB()
//------------------- Wanted Running Results : END --------
Sent via Deja.com http://www.deja.com/
Before you buy.
[ 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: Pete Becker <petebecker@acm.org>
Date: 1999/10/03 Raw View
Alex Vinokur wrote:
>
> In article <7t10cn$m9s$1@engnews1.eng.sun.com>,
> clamage@eng.sun.com (Steve Clamage) wrote:
> > Alex Vinokur <alexander.vinokur@telrad.co.il> writes:
> [snip]
> >
> > >Is it worth adding to C++ new kind of virtual methods
> > > (for instance, hard-virtual)
> > > that behave like virtual destructors?
> >
> > What behavior did you have in mind? Automatically call the
> > base-class function they override? Or something else?
> >
> //----- New kinds of virtuality ----------
> pre-virtual void foo2 () { IT_IS_ME; }
> post-virtual void foo3 () { IT_IS_ME; }
This looks like trouble. The implementor of a derived class has the
information needed to figure out whether an overriding function needs to
call the base version, and if so, when. Making those decisions in the
base class imposes constraints on the implementation of the derived
class, without enough information to be sure of making the right choice.
--
Pete Becker
Dinkumware, Ltd.
http://www.dinkumware.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: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/10/04 Raw View
Alex Vinokur wrote:
>
> In article <7t10cn$m9s$1@engnews1.eng.sun.com>,
> clamage@eng.sun.com (Steve Clamage) wrote:
> > Alex Vinokur <alexander.vinokur@telrad.co.il> writes:
> [snip]
> >
> > >Is it worth adding to C++ new kind of virtual methods
> > > (for instance, hard-virtual)
> > > that behave like virtual destructors?
> >
> > What behavior did you have in mind? Automatically call the
> > base-class function they override? Or something else?
> >
> [snip]
>
> I would like to get the following results.
>
> Alex
>
> //#########################################################
> //------------------- Wanted C++ code : BEGIN -------------
>
> #include <iostream>
>
> #define IT_IS_ME cout << __PRETTY_FUNCTION__ << endl;
> // __PRETTY_FUNCTION__ is predefined variable in the GNU compiler
>
> class BBB
> {
> public :
> virtual ~BBB () { IT_IS_ME; }
> virtual void foo1 () { IT_IS_ME; }
>
> //----- New kinds of virtuality ----------
> pre-virtual void foo2 () { IT_IS_ME; }
> post-virtual void foo3 () { IT_IS_ME; }
> //----------------------------------------
> };
>
> class DDD : public BBB
> {
> public :
> ~DDD () { IT_IS_ME; }
> void foo1 () { IT_IS_ME; }
> void foo2 () { IT_IS_ME; }
> void foo3 () { IT_IS_ME; }
> };
>
> int main ()
> {
> DDD ddd;
> cout << "\tOrdinary virtual method" << endl;
> ddd.foo1 ();
> cout << endl;
>
> cout << "\tpre-virtual method" << endl;
> ddd.foo2 ();
> cout << endl;
>
> cout << "\tpost-virtual method" << endl;
> ddd.foo3 ();
> cout << endl;
>
> cout << "\ttOrdinary virtual destructor" << endl;
>
> return 0;
> }
>
> //------------------- Wanted C++ code : END ---------------
>
> //#########################################################
> //------------------- Wanted Running Results : BEGIN ------
>
> Ordinary virtual method
> void DDD::foo1()
>
> pre-virtual method
> void BBB::foo2()
> void DDD::foo2()
>
> post-virtual method
> void DDD::foo3()
> void BBB::foo3()
>
> Ordinary virtual destructor
> DDD::~DDD()
> BBB::~BBB()
>
> //------------------- Wanted Running Results : END --------
You don't need a new language feature to achieve that result. The
following is only a little more complicated, and a lot more flexible:
> class BBB
> {
> public :
> virtual ~BBB () { IT_IS_ME; }
> virtual void foo1 () { IT_IS_ME; }
>
//----- New kinds of virtuality ----------
virtual void foo2_v();
virtual void foo3_v();
> void foo2 () { IT_IS_ME; foo2_v(); }
> void foo3 () { foo3_v(); IT_IS_ME; }
> //----------------------------------------
> };
>
> class DDD : public BBB
> {
> public :
> ~DDD () { IT_IS_ME; }
> void foo1 () { IT_IS_ME; }
> void foo2_v () { IT_IS_ME; }
> void foo3_v () { IT_IS_ME; }
> };
---
[ 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/01 Raw View
Alex Vinokur <alexander.vinokur@telrad.co.il> writes:
>Behavior of virtual methods and virtual destructors
> are different.
Destructors behave differently from ordinary member
functions because they destroy the object, and because
they automatically call their base-class versions. I don't
see how virtual destructors have any other differences.
>Is it worth adding to C++ new kind of virtual methods
> (for instance, hard-virtual)
> that behave like virtual destructors?
What behavior did you have in mind? Automatically call the
base-class function they override? Or something else?
--
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: AllanW <allan_w@my-deja.com>
Date: 1999/10/01 Raw View
In article <7t10cn$m9s$1@engnews1.eng.sun.com>,
clamage@eng.sun.com (Steve Clamage) wrote:
> Alex Vinokur <alexander.vinokur@telrad.co.il> writes:
> >Is it worth adding to C++ new kind of virtual methods
> > (for instance, hard-virtual)
> > that behave like virtual destructors?
>
> What behavior did you have in mind? Automatically call the
> base-class function they override? Or something else?
That would have been a nice provision when virtual methods
was originally designed. Forgetting to call a base class
method (when that's the appropriate thing to do) is a
common-enough mistake. With two different types of virtual
functions, we could indicate that some methods are meant
to be REPLACED in derived classes (which is what we have
now), while others are meant to ADD to the base class logic.
Then authors of derived classes wouldn't have to look up
which way to proceed.
I can't imagine that it's worth changing the language for it,
though. After all, calling the base class method isn't all
that difficult to do. This also gives the derived class
author the flexibility to call the base class either BEFORE
or AFTER the custom code, and the ability to choose any one
of the overloaded versions. We also have the ability to
recognize that this one class is an exception -- we don't
want to call the base class after all, even though it said
that we normally should. We probably wouldn't have any of
those things if the language enforced an automatic
call-the-base-class concept for virtual functions.
--
Allan_W@my-deja.com is a "Spam Magnet," never read.
Please reply in newsgroups only, sorry.
Sent via Deja.com http://www.deja.com/
Before you buy.
[ 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: Alex Vinokur <alexander.vinokur@telrad.co.il>
Date: 1999/09/30 Raw View
Hi,
Behavior of virtual methods and virtual destructors
are different.
Is it worth adding to C++ new kind of virtual methods
(for instance, hard-virtual)
that behave like virtual destructors?
Alex
Sent via Deja.com http://www.deja.com/
Before you buy.
[ 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 ]