Static Read-only array fields in C++

G

Guest

I can't create a Constant/Read-only array field in managed C++ classes -
doesn't allow the keyword const pointer to const object on array fields in
managed C++ classes. e.g. Want to define a read/only field for an empty array
(so all the empty arrays of the given type can use the same object) however
because the pointer in the field cannot be marked as constant then it is
possible that any external party can alter this pointer to reference a new
object.

public __gc class ExampleClass
{
public:
ExampleClass();

// NOT a static read-only field - anyone can assign a new value to this
field
// e.g. ExampleClass::EmptyObjsArray = NULL
static MyObj * EmptyObjsArray __gc []; // can't add const to any part of
the declaration to make the field read-only
}

Does anyone know how to do this in C++? I know you can easily do the same
thing in C#. The problem is only with managed arrays. Can easily create
static read-only fields if the type is NOT an array. e.g.

public __gc class ExampleClass2
{
public:
ExampleClass2();

static MyObj * const OneObj = new MyObj();
};

Any ideas?
 
T

Tomas Restrepo \(MVP\)

Hi Mark,
I can't create a Constant/Read-only array field in managed C++ classes -
doesn't allow the keyword const pointer to const object on array fields in
managed C++ classes. e.g. Want to define a read/only field for an empty array
(so all the empty arrays of the given type can use the same object) however
because the pointer in the field cannot be marked as constant then it is
possible that any external party can alter this pointer to reference a new
object.

public __gc class ExampleClass
{
public:
ExampleClass();

// NOT a static read-only field - anyone can assign a new value to this
field
// e.g. ExampleClass::EmptyObjsArray = NULL
static MyObj * EmptyObjsArray __gc []; // can't add const to any part of
the declaration to make the field read-only
}

Does anyone know how to do this in C++? I know you can easily do the same
thing in C#. The problem is only with managed arrays. Can easily create
static read-only fields if the type is NOT an array. e.g.

I don't believe there is a way to mark as read-only a field as it is in C#.
This basically is done through the initonly mark in the field's metadata,
but I don't think it is currently supported in the current MC++ syntax.
Here's an example of what it would look like in the new Syntax:

public ref class ExampleClass
{
public:
ExampleClass() { };

static initonly array<String^>^ EmptyObjsArray = gcnew array<String^>(3);

void DoIt()
{
EmptyObjsArray = gcnew array<String^>(2); // error
/*
t.cpp(15) : error C3894: 'EmptyObjsArray' : l-value use of initonly static
data
member is only allowed in the class constructor of class 'ExampleClass'
*/
}
};
 
G

Guest

Tomas,

Thanks fior the reply. I had already suspected that this was impossible in
C++ .NET 2003.

Just to clarify, this appears only to be a problem relating to managed
arrays. With other types of objects, the 'const' keyword in the appropriate
place seems to do what I want. (The object is still mutable but the 'pointer'
isn't.)
If not using arrays, does the 'const' keyword actually result in the correct
'initonly' entry in the metadata, or is it just 'const' as far as the C++
compiler is concerned but external consumers of this class, implemented in
different languages, would actually be able to modify this public field.

Also, any idea why this is an issue with managed arrays, and not other
managed type in C++? Is this just a quirk with the way the managed array
syntax was implemented in Managed C++?

Regards,

Mark

Tomas Restrepo (MVP) said:
Hi Mark,
I can't create a Constant/Read-only array field in managed C++ classes -
doesn't allow the keyword const pointer to const object on array fields in
managed C++ classes. e.g. Want to define a read/only field for an empty array
(so all the empty arrays of the given type can use the same object) however
because the pointer in the field cannot be marked as constant then it is
possible that any external party can alter this pointer to reference a new
object.

public __gc class ExampleClass
{
public:
ExampleClass();

// NOT a static read-only field - anyone can assign a new value to this
field
// e.g. ExampleClass::EmptyObjsArray = NULL
static MyObj * EmptyObjsArray __gc []; // can't add const to any part of
the declaration to make the field read-only
}

Does anyone know how to do this in C++? I know you can easily do the same
thing in C#. The problem is only with managed arrays. Can easily create
static read-only fields if the type is NOT an array. e.g.

I don't believe there is a way to mark as read-only a field as it is in C#.
This basically is done through the initonly mark in the field's metadata,
but I don't think it is currently supported in the current MC++ syntax.
Here's an example of what it would look like in the new Syntax:

public ref class ExampleClass
{
public:
ExampleClass() { };

static initonly array<String^>^ EmptyObjsArray = gcnew array<String^>(3);

void DoIt()
{
EmptyObjsArray = gcnew array<String^>(2); // error
/*
t.cpp(15) : error C3894: 'EmptyObjsArray' : l-value use of initonly static
data
member is only allowed in the class constructor of class 'ExampleClass'
*/
}
};
 
T

Tomas Restrepo \(MVP\)

Hi Mark,
Thanks fior the reply. I had already suspected that this was impossible in
C++ .NET 2003.

Just to clarify, this appears only to be a problem relating to managed
arrays. With other types of objects, the 'const' keyword in the appropriate
place seems to do what I want. (The object is still mutable but the 'pointer'
isn't.)
If not using arrays, does the 'const' keyword actually result in the correct
'initonly' entry in the metadata, or is it just 'const' as far as the C++
compiler is concerned but external consumers of this class, implemented in
different languages, would actually be able to modify this public field.

It depends on where you put it :)
Example, this:
static String * const empty = S"asdasd";
will generate "initonly". This, otoh:
static const String * empty = S"asdasd";
won't :)

Also, any idea why this is an issue with managed arrays, and not other
managed type in C++? Is this just a quirk with the way the managed array
syntax was implemented in Managed C++?

I'm not quite sure exactly what the issue is, someone from MS would've to
chime in to answer exactly... It's probably a quirk in the front end, I
guess....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top