Private Virtual Functions

The way private virtual functions are handled in derived classes has changed from Managed Extensions for C++ to Visual C++ 2008.

In Managed Extensions, the access level of a virtual function does not constrain its ability to be overridden within a derived class. In the new syntax, a virtual function cannot override a base class virtual function that it cannot access. For example:

__gc class MyBaseClass {
   // inaccessible to a derived class 
   virtual void g();
};

__gc class Bar : public MyBaseClass {
public:
   // in Managed Extensions, ok: g() overrides MyBaseClass::g()
   // in new syntax, error: cannot override: MyBaseClass::g() is inaccessible …
   void g();
};

There is no real mapping of this sort of design onto the new syntax. One simply has to make the base class members accessible – that is, non-private. The inherited methods do not have to bear the same access. In this example, the least invasive change is to make the MyBaseClass member protected. This way the general program's access to the method through MyBaseClass is still prohibited,

ref class MyBaseClass {
protected:
   virtual void g();
};

ref class Bar : MyBaseClass {
public:
   virtual void g() override;
};

Note that the absence of the explicit virtual keyword in the base class, under the new syntax, generates a warning message.

See Also

Concepts

Member Declarations within a Class or Interface

Reference

Member Visibility