Search code examples
c#c++-climanaged-c++

C++/CLI abstract base class?


I'm trying to write a mixed assembly. Here's a snippet:

public ref class OpusStream
: public Stream
{
protected:
    OpusStream(void);

public:
    ~OpusStream(void);
    !OpusStream(void);
    int TestEncode(void);

    /* System::IO::Stream */
    virtual property bool CanRead { bool get(void) override; }
    virtual property bool CanSeek { bool get(void) override; }
    virtual property bool CanWrite { bool get(void) override; }
    virtual property __int64 Length { __int64 get(void) override; }
    virtual property __int64 Position {
        __int64 get(void) override;
        void set(__int64) override;
    }
    virtual void Flush(void) override;
    int Read(Byte[], int, int) override;
    long Seek(long, System::IO::SeekOrigin) override;
    void SetLength(long) override;
    void Write(Byte[], int, int) override;
};

And this is the compiler error:

Error 11 Cannot create an instance of the abstract class or interface 'OpusNet.OpusStream' C:\Users\benc\Documents\opus-1.1-alpha\OpusTests\MainWindow.xaml.cs 30 21 OpusTests

It's been a while since I've done C++/CLI. I'm really not sure what I've missed here.


Solution

  • The Read and Write overrides were using the wrong type for (as per C#) byte[].

    For the overrides to actually override the methods on stream you need to use the correct type, which, in a roundabout way I discovered by tinkering with override omitted until the compiler told me I was hiding a method on System.IO.Stream:

    So;

    typedef array<Byte>^ ByteArray;
    

    Then;

    virtual __int32 Read(ByteArray, __int32, __int32) override;