Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all;

Can you have an array as a property of a managed class in Microsoft C++
without having to use the gcnew function to allocate memory for the array?

This will not compile:

public ref class foo_class
{ int array01[22]; }

Is there another way to get the array to be within the same block of memory
as that for the instance of the class?

Can this be done in any way other than having to do the following?:

public ref class foo_class
{
array <int> ^ array01;

foo_class ()
{ array01 = gcnew <int> (22); }
}

Thanks!!!
Posted

I doubt it. You could perhaps use a .NET container, which then gives you the ToArray method, if you don't want to call gcnew yourself.
 
Share this answer
 
Hi Christian;

So, for every array that is to be a property of a class,
I have to allocate additional buffer space for each and every
array within the class/instance?


public ref class foo_class
{
array [int] ^ array01;
array [int] ^ array02;
array [long] ^ array03;
array [char] ^ array04;

foo_class ()
{
array01 = gcnew [int] (22);
array02 = gcnew [int] (33);
array03 = gcnew [long] (7);
array04 = gcnew [char] (333);
}
}


There is no way to allocate all of the space within one block
(for the instance along with space for any and all arrays within)?

Thanks for the response!!!
 
Share this answer
 
This is a little over my head, I'm going to ask Nish to look at it, he's the expert on this stuff.
 
Share this answer
 
You cannot initialize a non-static member field in C++/CLI (same rules as in standard C++). I suppose you're trying to do the equivalent of this C# code :
C#
class Test
{
    int[] arr = new int[10];
}

But if you look at the compiled IL, you'll see that the C# compiler has generated this code in the constructor :
C#
{
...
    this.arr = new int[10];
...
}

So unfortunately you have to do it as you do now :
MC++
ref class Ref
{
    array<int>^ arr;
public:
    Ref()
    {
        arr = gcnew array<int>(10);
    }
};
 
Share this answer
 
Hi Nishant;

- - - - - - - - - - - - - - - - - - - - - - - - - -
You cannot initialize a non-static member field in C++/CLI (same rules as
in standard C++). I suppose you're trying to do the equivalent of this C# code :

class Test
{
int[] arr = new int[10];
}

- - - - - - - - - - - - - - - - - - - - - - - - - -

No - that is not what I am trying to do.

All I am trying to do is to declare a static array as a member (property)
of a managed class - like this:

public ref class xClass01
{
int array01[22];
};


When I compile this, I get the following diagnostic:

Test-01.cpp
Test-01.cpp(24) : error C4368: cannot define 'array01' as a member of
managed 'testing::xClass01': mixed types are not supported


Thanks for the response!!!
 
Share this answer
 
Well these two are two completely different entities :

int arr[10]


and

array<int>^ arr;


The former is a fixed native array, the latter is a managed array type.

If you have the need to embed multiple fixed array fields, you can put them in a native struct or class, and then declare a pointer member in the ref class. Something like this :

C++
class Native
{
    int arr1[25];
    int arr2[20];
    int arr3[15];
};

ref class Ref
{
    Native* pN;

public:
    Ref()
    {        
        pN = new Native();
    }

    ~Ref()
    {
        this->!Ref();
    }

    !Ref()
    {
        delete pN;
    }
};
 
Share this answer
 
Hi Nishant;

- - - - - - - - - - - - - - - - - - - - - - - - - -

Well these two are two completely different entities :

int arr[10]

and

array<int>^ arr;



The former is a fixed native array, the latter is a managed array type.

If you have the need to embed multiple fixed array fields, you can put them in a native struct or class, and then declare a pointer member in the ref class. Something like this :

class Native
{
int arr1[25];
int arr2[20];
int arr3[15];
};

ref class Ref
{
Native* pN;

public:
Ref()
{
pN = new Native();
}

~Ref()
{
this->!Ref();
}

!Ref()
{
delete pN;
}
};

- - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - -

I'm not sure there would be much of an advantage in doing that.

My understanding of arrays in C/C++ is that the indexing value within the
square braces is a relative offset from a base address. I make no claim to
having much of an idea as to how the managed heap works, so perhaps I'm
being a bit naive in expecting for those indexing values to effect the same
functionality within the managed heap, and I'm confounded in my own lack of
understanding as to the reasons why it would be difficult (if not impossible)
to implement that functionality. Maybe I shouldn't be so shocked and stunned
in that regard, but I am nonetheless.

Whatever the case, if no one else has any better ideas, I'll just trudge
along with allocating dynamic arrays from the managed heap, and just take
what I can get in that regard.

I want to extend my thanks to you (Nishant), and to everyone who responded
to this thread.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900