C ++ / CLI, "persistent" reference to a tracking descriptor
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;
}
, , ++, ++ ++/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);
}
};