This is going to be a really goofy question but is it possible to do the following in C++/CLI?
// C++/CLI
public ref class Managed
{
public:
array<double>^ m_data;
Managed(array<double>^% data) : m_data(data)
{
}
void bar(int x)
{
System::Array::Resize(m_data, x);
}
};
// C#
static void Main(string[] args)
{
double [] d = new double[10];
Foo.Managed f = new Foo.Managed(d);
f.bar(5);
}
Such that, after calling f.bar(5) from Main, Main::d and f.m_data are the same "reallocated" array? I've tried with unmanaged references, pointers, pin_ptr and so forth but nothing. Any ideas? If this is just not possible, is there a reason why?
Using the reference tracking operator (%) I could do that if I would to resize in the constructor, that's what it has kept me trying.
This is possible in C++ using references, unfortunately I don't know how to cast a array^ to array^& nor I can use array^% as a member:
class Foo{
public:
double *& m_data;
Foo(double*& data): m_data(data)
{
}
void bar(int x)
{
delete m_data;
m_data = new double[x];
}
~Foo(){
delete [] m_data;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
double* data = new double[10];
Foo f(data);
std::cout << &data << "\n" << &f.m_data << "\n";
f.bar(5);
std::cout << &data << "\n" << &f.m_data;
getchar();
return 0;
}