^ const & list ) { ...">

C ++ / CLI, "persistent" reference to a tracking descriptor

I noticed something like this in the code:

void foo(IList<int>^ const & list ) { ... }

What does that mean ^ const&? I looked at the C ++ / CLI specification, but did not find comments about creating permalinks to tracking or command ^&.

Is it legal?

+3
source share
2 answers

This is the link that is constant for the tracking descriptor.

It allows you to pass a descriptor by reference instead of a value. Presumably, the author considers this more efficient than copying a descriptor.

If the author wanted to make the descriptor permanent, he should use any of

Method(TestClass const ^ const & parameter) 
Method(TestClass const^ parameter)

, Method(TestClass const^& parameter) - TestClass const^ constHandle = nonConstHandle

:

// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

ref class TestClass
{

public:

    void setA(int value)
    {
        a = value;
    }



    TestClass() : 
        a(10)
    {
    }

private:    

    int a;
};

class TakesHandle
{
public:

    void methodX1(TestClass const ^ const & parameter)
    {
    // Un-commenting below causes compiler error
    // parameter->setA(11);
    }

    void methodX2(TestClass const^ parameter)
    {
    // Un-commenting below causes compiler error
    // parameter->setA(11);
    }

    void methodX3(TestClass const^& parameter)
    {
    // Un-commenting below causes compiler error
    // parameter->setA(11);
    }

};


int _tmain(int argc, _TCHAR* argv[])
{
    TakesHandle takes;

    TestClass ^ test1 = gcnew TestClass();

    // compiles
    takes.methodX1(test1);

    // compiles
    takes.methodX2(test1);

    TestClass const ^ constHandle = test1;
    takes.methodX3(constHandle);

    return 0;
}
+2

, , ++, ++ ++/CLI. , , . , List < > , . . ^ , .

const . , - , CLR . , .NET. .

, , :

 void foo(IList<int>^ list ) { ... }

, , :

using namespace System;
using namespace System::Collections::Generic;

ref class Test {
public:
    IList<int>^ lst;
    void foo(IList<int> const &list) {}
    void wontcompile() {
        foo(lst);  // C3699
        IList<int>^ okay;
        foo(okay);
    }
};
+9

Source: https://habr.com/ru/post/1744788/


All Articles