Topic: overriding private virtual functions


Author: Avi Kak <kak@purdue.edu>
Date: Fri, 1 Nov 2002 11:28:19 CST
Raw View
Folks,

  I had believed that the standard did not allow a private virtual
function to behave polymorphically.  Yet, the following example shows
foo() behaving polymorphically
even though it is in the private section of the base class X.   Is my
understanding of the standard erroneous?  The program was compiled with
g++ (2.95 and 2.96).

Avi Kak


#include <iostream>
using namespace std;

class X {
    int m;
    virtual void foo()  { cout << "X's foo invoked" << endl; }
public:
    X( int mm ) : m( mm ) {}
    void bar() { foo(); }
};


class Y : public X {
    int n;
    void foo() { cout << "Y's foo invoked" << endl; }
public:
    Y( int mm, int nn ) : X( mm ), n( nn ) {}
};


int main() {
    X* p = new Y( 10, 20 );
    p->bar();                 //output: Y's foo invoked
    return 0;
}



---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Victor Bazarov" <vAbazarov@dAnai.com>
Date: Fri, 1 Nov 2002 11:54:06 CST
Raw View
"Avi Kak" <kak@purdue.edu> wrote...
>   I had believed that the standard did not allow a private virtual
> function to behave polymorphically.

There is nothing in the Standard that would prohibit polymorphic
invocation of a virtual function.  Why had you believed that?

>  Yet, the following example shows
> foo() behaving polymorphically
> even though it is in the private section of the base class X.   Is my
> understanding of the standard erroneous?

It seems so, yes.

>  The program was compiled with
> g++ (2.95 and 2.96).

And it should compile with any other compliant compiler.  You do
have a memory leak, which can be dealt with by introducing the
virtual d-tor into 'X' and calling 'delete p' before the 'return'
in 'main', but that's not such a bid deal, as I understand.

>
> Avi Kak
>
>
> #include <iostream>
> using namespace std;
>
> class X {
>     int m;
>     virtual void foo()  { cout << "X's foo invoked" << endl; }
> public:
>     X( int mm ) : m( mm ) {}
>     void bar() { foo(); }
> };
>
>
> class Y : public X {
>     int n;
>     void foo() { cout << "Y's foo invoked" << endl; }
> public:
>     Y( int mm, int nn ) : X( mm ), n( nn ) {}
> };
>
>
> int main() {
>     X* p = new Y( 10, 20 );
>     p->bar();                 //output: Y's foo invoked
>     return 0;
> }

Victor
--
Please remove capital A's from my address when replying by mail


---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Ron Natalie" <ron@sensor.com>
Date: Fri, 1 Nov 2002 13:40:18 CST
Raw View
"Avi Kak" <kak@purdue.edu> wrote in message news:3DC21E5E.50D3DD75@purdue.edu...
>
> Folks,
>
>   I had believed that the standard did not allow a private virtual
> function to behave polymorphically.

Access control does not affect polymorphism.

The access is checked in the context of the static type at the point of call.
Therefore the access of the overridden function is not significant.



> int main() {
>     X* p = new Y( 10, 20 );
>     p->bar();                 //output: Y's foo invoked

p->bar() is public.   It matters not what the dynanmic type of *p is.




---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: dickie@acm.org (Garth A. Dickie)
Date: Sun, 3 Nov 2002 17:37:17 +0000 (UTC)
Raw View
"Avi Kak" <kak@purdue.edu> wrote
> I had believed that the standard did not allow a private virtual
> function to behave polymorphically.

"Victor Bazarov" <vAbazarov@dAnai.com> wrote
> There is nothing in the Standard that would prohibit polymorphic
> invocation of a virtual function.  Why had you believed that?

In fact this is a useful idiom:

A private virtual function can be called by the base class, to provide
polymorphic behavior, but does not become part of the callable public
or protected interface to that class.

For example, in thread classes I usually make the run() method private
virtual.  The run() method is called by the thread mechanism when the
thread is started, and should not be considered part of the control
interface for threads:

class thread {
public:
    ....

    void start();
    bool wait(ulong timeout);
    void kill();
    void kill_for_destructor(); // don't throw

private:
    virtual void run() = {}

private:
    ....
};

Derived classes then implement run().

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]