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

后端 未结 3 1176
难免孤独
难免孤独 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:24

    Using Visual Studio 2008, this works and solved a major problem at my job. Thanks!

    // header
    // Use namespace for Out-attribute.
    using namespace System::Runtime::InteropServices; 
    namespace VHT_QMCLInterface {
       public ref class Client
       {
        public:
            Client();
            void ReturnInteger( int a, int b, [Out]int %c);
            void ReturnString( int a, int b, [Out]String^ %c);
       }
    }
    
    // cpp
    namespace VHT_QMCLInterface {
    
        Client::Client()
        {
    
        }
    
        void Client::ReturnInteger( int a, int b, [Out]int %c)
        {
            c = a + b;
        }
        void Client::ReturnString( int a, int b, [Out]String^ %c)
        {
            c = String::Format( "{0}", a + b);
        }
    }
    
    // cs
    namespace TestQMCLInterface
    {
        class Program
        {
            VHT_QMCLInterface.Client m_Client = new VHT_QMCLInterface.Client();
            static void Main(string[] args)
            {
                Program l_Program = new Program();
                l_Program.DoReturnInt();
                l_Program.DoReturnString();
                Console.ReadKey();
            }
    
            void DoReturnInt()
            {
                int x = 10;
                int y = 20;
                int z = 0;
                m_Client.ReturnInteger( x, y, out z);
                Console.WriteLine("\nReturnInteger: {0} + {1} = {2}", x, y, z);
            }
    
            void DoReturnString()
            {
                int x = 10;
                int y = 20;
                String z = "xxxx";
                m_Client.ReturnString(x, y, out z);
                Console.WriteLine("\nReturnString: {0} + {1} = '{2}'", x, y, z);
            }
         }
    }
    
    0 讨论(0)
  • 2020-11-30 03:31

    It's not supported. The closest you can get is ref

    Granted you can fake it, but you lose a compile time check.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题