How to declare the default indexed property in C++/CLI interface

浪子不回头ぞ 提交于 2021-01-27 20:31:01

问题


how is it possible to declare the default indexed property in an C++/CLI - Interface.
(Please excuse the repeating, full qualified notation with namespaces because I'm just learning C++/CLI and want to be sure that no acciential mixup of language primitives between C++ and C# happens)

Code is

public interface class ITestWithIndexer
{
    property System::String ^ default[System::Int32];
}

Compiler always throws "error C3289: 'default' a trivial property cannot be indexed".
Where is my error?

PS: in C#, it would simply be

public interface ITestWithIndexer
{
    System.String this[System.Int32] { get; set; }
}

How to translate this into C++/CLI?

Thanks!!


回答1:


In C++/CLI, a 'trivial' property is one where the getter & setter is not declared. With a non-trivial property, the getter & setter are declared explicitly, with syntax that's more like a normal method declaration than C#'s property syntax.

public interface class IThisIsWhatANonIndexedNonTrivialPropertyLooksLike
{
    property String^ MyProperty { String^ get(); void set(String^ value); }
};

Since the trivial syntax isn't allowed for indexed properties, we need to do that for your indexed property.

public interface class ITestWithIndexer
{
    property String^ default[int]
    {
        String^ get(int index); 
        void set(int index, String^ value);
    }
};

Here's my test code:

public ref class TestWithIndexer : public ITestWithIndexer
{
public:
    property String^ default[int] 
    {
        virtual String^ get(int index)
        {
            Debug::WriteLine("TestWithIndexer::default::get({0}) called", index);
            return index.ToString();
        }
        virtual void set(int index, String^ value)
        {
            Debug::WriteLine("TestWithIndexer::default::set({0}) = {1}", index, value);
        }
    }
};

int main(array<System::String ^> ^args)
{
    ITestWithIndexer^ test = gcnew TestWithIndexer();
    Debug::WriteLine("The indexer returned '" + test[4] + "'");
    test[5] = "foo";
}

Output:

TestWithIndexer::default::get(4) called
The indexer returned '4'
TestWithIndexer::default::set(5) = foo



回答2:


A "trivial property" is one where the compiler can auto-generate a getter and setter, deducing them from the property declaration. That cannot work for an indexed property, the compiler has no idea what it is supposed to do with the index variable. You must therefore declare the getter and setter explicitly. Not unlike the C# declaration, minus the syntax sugar. Ecma-372, chapter 25.2.2 has an example. Adapted to your case:

public interface class ITestWithIndexer {
    property String ^ default[int] { 
        String^ get(int);
        void set(int, String^);
    }
};


来源:https://stackoverflow.com/questions/27804125/how-to-declare-the-default-indexed-property-in-c-cli-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!