Subject: Re: "gnutls" under Darwin (was: CVS commit: pkgsrc/security/gnutls)
To: Matthias Scheler <tron@zhadum.org.uk>
From: Todd Vierling <tv@pobox.com>
List: tech-pkg
Date: 01/24/2007 16:25:00
On 1/24/07, Matthias Scheler <tron@zhadum.org.uk> wrote:
> It seems to be a common problem which is related to using pure virtual
> method in a constructor or destructor, see here:
>
> http://porting.openoffice.org/mac/macosx_issues.html

The code example there is, at best, buggy (if not completely wrong).
The method getClass() as shown is returning an instance of, not a
pointer or reference to, "MyAbstractClass":

=====
class MyWrapperClass
{
[...]
    static MyAbstractClass getClass();
};

MyAbstractClass MyWrapperClass::getClass()
{
    static MyConcreteClass rClass;
    return rClass;
}
=====

The return of getClass() as written above should *never* return a
MyConcreteClass instance, because the return value specifies a
concrete instance of only MyAbstractClass.  Rather, the correct
operation here is for the method to call the (autogenerated) copy ctor
of MyAbstractClass to copy only that class's members into the return
value (an instance of MyAbstractClass only).

Someone apparently needs to reread Stroustrup.

The following code:

=====
MyWrapperClass::~MyWrapperClass()
{
    MyAbstractClass& aClass = getClass();
    aClass.print();
}
=====

will then call the pure virtual method print() by definition, because
the vtable pointing to MyConcreteClass doesn't even *exist* in the
aClass variable; aClass is only an instance of MyAbstractClass.  The
behavior of calling a pure virtual method in this manner is specified
as a condition requiring program abort.

-- 
-- Todd Vierling <tv@duh.org> <tv@pobox.com> <todd@vierling.name>