The following BaseClass defines an abstract class with a public Description property that invokes a pure, virtual description() method. The DerivedClass is a sub-class of BaseClass and implements the virtual description() method. I want the description() method to be private, but when I try to create a DerivedClass, the compiler balks, calling DerivedClass abstract because of the description() method. When I declare the description() method as protected or public, it compiles and runs without errors.
Having a public interface and a private implementation is one of many ideas I've used from Herb Sutter's books. This works fine in native classes, just not ref classes. Any ideas
ref class BaseClass abstract
{
public:
property String^ Description
{
String^ get()
{
return description();
}
}
//private:
protected:
// Gets the description using some formula.
virtual String^ description() = 0;
};
ref class DerivedClass : public BaseClass
{
//private:
protected:
virtual String^ description() override
{
return "Some String";
}
};

Cannot compile abstract ref classes with private pure virtual methods
_ilya_
Removing the pure virtual function BaseClass::description will fix your problem. private abstract functions don't make much sense, since you can't polymorphically access them through the abstract class. I'd say that this restriction is by design (why have all .NET languages support something that has no practical use.)
I have a feeling that Sutter meant that the private stuff goes into the implementation class, not the abstract class.
Brian
mklynx
I found a reference in the MSDN Library C++ porting and upgrading section that explains the problem. Private virtual methods have been removed from C++/CLI, but are still valid in native code or Managed Extensions:
The way private virtual functions are handled in derived classes has changed from Managed Extensions for C++ to Visual C++ 2005.
In Managed Extensions, the access level of a virtual function does not constrain its ability to be overridden within a derived class. This has changed. In the new syntax, a virtual function cannot override a base class virtual function that it cannot access.
Declaring the methods as protected accomplishes my design goal, a public non-changing interface with a non-public polymorphic implementation.
Thanks for your help.