我遇到的问题是我正在创建一个派生自抽象类TextWriter的类。
文档 http://msdn.microsoft.com/en-us/library/System.IO.TextWriter(v=vs.110).aspx
来源 http://referencesource.microsoft.com/#mscorlib/system/io/textwriter.cs
所有源代码都在C#中,并且在160-162行上有一个特定的部分,我需要覆盖派生类,使其不被视为抽象(由编译器警告暗示):
public abstract Encoding Encoding {
get;
}
我如何将这一部分翻译成C ++来覆盖它?
警告声明'System :: Text :: Encoding ^ System :: IO :: TextWriter :: Encoding :: get(void)'是抽象的(如TextWriter的源代码所示),但同时当我尝试覆盖这个“get”函数时,我得到的错误是我的类包含显式覆盖'get',但不是从包含函数声明的接口派生的。
这是我所拥有的这个函数的C ++变体的唯一想法,它告诉我基类中不存在“get”函数:
System::Text::Encoding^ Encoding::get(void) {
return nullptr;
}
显示此问题的最基本示例:
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;
}
答案 0 :(得分:5)
如果您在property
的定义中使用SampleWriter
语法,该怎么办?像这样:
property System::Text::Encoding^ Encoding {
virtual System::Text::Encoding^ get(void) override {
return nullptr;
}
}