In C++/CLI, how do I declare and call a function with an 'out' parameter?

后端 未结 3 1242
难免孤独
难免孤独 2020-11-30 03:03

I have a function which parses one string into two strings. In C# I would declare it like this:

void ParseQuery(string toParse, out string search, out strin         


        
3条回答
  •  眼角桃花
    2020-11-30 03:47

    C++/CLI itself doesn't support a real 'out' argument, but you can mark a reference as an out argument to make other languages see it as a real out argument.

    You can do this for reference types as:

    void ReturnString([Out] String^% value)
    {
       value = "Returned via out parameter";
    }
    
    // Called as
    String^ result;
    ReturnString(result);
    

    And for value types as:

    void ReturnInt([Out] int% value)
    {
       value = 32;
    }
    
    // Called as
    int result;
    ReturnInt(result);
    

    The % makes it a 'ref' parameter and the OutAttribute marks that it is only used for output values.

提交回复
热议问题