Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++/CLI
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 ^ array01;
 
foo_class ()
{ array01 = gcnew (22); }
}
 
Thanks!!!
Posted 26-Jan-10 7:41am
wylbur246
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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!!!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

This is a little over my head, I'm going to ask Nish to look at it, he's the expert on this stuff.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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];
}
But if you look at the compiled IL, you'll see that the C# compiler has generated this code in the constructor :
{
...
    this.arr = new int[10];
...
}
So unfortunately you have to do it as you do now :
ref class Ref
{
    array<int>^ arr;
public:
    Ref()
    {
        arr = gcnew array<int>(10);
    }
};
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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!!!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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;
    }
};
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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.
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 27 Jan 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100