Inference from .NET Abstract Class in C ++, System :: IO :: TextWriter

I am having a problem when I am making a class derived from the abstract TextWriter class.

Documentation http://msdn.microsoft.com/en-us/library/System.IO.TextWriter(v=vs.110).aspx

Source http://referencesource.microsoft.com/#mscorlib/system/io/textwriter.cs

So all the source code is in C # and on lines 160-162 there is this part that I need to override so that the derived class is not considered abstract (hint at a compiler warning):

public abstract Encoding Encoding {
    get;
}

      

How would I translate this part to C ++ in order to override it?

The warning states that "System :: Text :: Encoding ^ System :: IO :: TextWriter :: Encoding :: get (void)" is abstract (as shown in the source TextWriter), but at the same time when I try to override this "get" function, I get an error when my class contains an explicit override of "get" but does not come from the interface containing the function declaration.

Here is the only idea for the C ++ variant of this function, which pointed out to me that the "get" function does not exist in the base class:

  System::Text::Encoding^ Encoding::get(void) {
    return nullptr;
  }

      

The simplest example showing this problem:

public ref class SampleWriter : public System::IO::TextWriter {
public:

  SampleWriter() {
    // Do Nothing.
  }

  // Need to override 'System::Text::Encoding ^System::IO::TextWriter::Encoding::get(void)'... 
  // Remove this function to see that SampleWriter is inherently abstract and cannot be initialized
  virtual System::Text::Encoding^ Encoding::get(void) override {
    return nullptr;
  }

protected:
  ~SampleWriter() {
    // Do nothing.
  }
};

public ref class SomeForm : public System::Windows::Forms::Form {
public:
  SomeForm(void) {
    InitializeComponent();
    sample_writer = gcnew SampleWriter();
  }

protected:
  ~SomeForm() {
    if (components)
      delete components;
  }

private:
  System::ComponentModel::Container^ components;
  SampleWriter^ sample_writer;

  void InitializeComponent(void) {
    this->components = gcnew System::ComponentModel::Container();
    this->Size = System::Drawing::Size(300, 300);
  }
};

int main() {
  SomeForm^ form = gcnew SomeForm();
  form->ShowDialog();

  return 0;
}

      

+3


source to share


1 answer


What if you use syntax property

in the definition SampleWriter

? Something like that:



property System::Text::Encoding^ Encoding {
    virtual System::Text::Encoding^ get(void) override {
        return nullptr;
    }
}

      

+5


source







All Articles