Search code examples
clr

Unable to override .NET abstract class in CLR/C++


I have logger.cs:

public abstract class LogProvider
{
    protected abstract void DoLog(LogEvent logEvent);
}

my_code.cpp

ref class DebugLogProvider : public LogProvider
{
public:

    virtual void DoLog(LogEvent logEvent)
    {
        Console::WriteLine(gcnew System::String("fsdfsdf")); 
    }
};

I got error:

error C2259: '`anonymous-namespace'::DebugLogProvider': cannot instantiate abstract class
message : 'void LogProvider::DoLog(LogEvent ^)': is abstract

How to override DoLog ?


Solution

  • It seems the LogProvider class has a pure virtual function DoLog() that is declared as abstract without an implementation. Any derived class that inherits from LogProvider must implement DoLog().

    In your DebugLogProvider class, you need to implement the DoLog() function to override the pure virtual function in the base class. e.g:

    ref class DebugLogProvider : public LogProvider
    {
    public:
        virtual void DoLog(LogEvent^ logEvent) override
        {
            Console::WriteLine(gcnew System::String("fsdfsdf")); 
        }
    };