473,624 Members | 2,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static Read-only array fields in C++

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::E mptyObjsArray = 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?
Nov 17 '05 #1
3 5505

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::E mptyObjsArray = 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'
*/
}
};

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #2
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)" wrote:

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::E mptyObjsArray = 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'
*/
}
};

--
Tomas Restrepo
to****@mvps.org

Nov 17 '05 #3
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....

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9505
by: Rahul Joshi | last post by:
Hi, Is it possible to define static member functions that are 'const', i.e. they just read but do not modify the static data members of a class? Declaring functions like: class SomeClass { static int read() const; };
3
2228
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. Someone on the SuSE programming mailing list suggested my problem is that I'm trying to execute a function (I assume he meant the constructor) at compile time. The same source code compile if I don't try to split it up into separate libraries. ...
19
24572
by: Mike Ruane-Torr | last post by:
Why can't I have a static abstract method in C#? My intention is to have a class-level method that returns a string to supply information about inherited classes, and it is natural to make this static so that I don't need an instance in order to call it. However, because my class model is using a common base class, I need to make it abstract too, so that inherited classes are forced to implement it. Am I doing something that can be...
10
6354
by: Paschalis Pagonidis | last post by:
Hi, I have a class which all its attributes, properties and methods are static. However, I want a method to return an object that should be non-static. Is that possible?
13
14606
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and Nicolai M. Josuttis. Chapter 14.) How to implement analogue of this technique via static polymorphism? Perhaps there is special design pattern for this purpose...
15
3887
by: eminemence | last post by:
Hi, Is it possible to implement a Singleton without using static variables or global variables. Thanks. --eminemence.
55
6190
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
5
3049
by: Andrew Robinson | last post by:
I would think that I could do this but apperently not. "'Class1' already defines a member called 'Read' with the same parameter types" public class Class1 { public void Read() { } public static void Read() { new Class1().Read(); }
2
1994
by: Tim Van Wassenhove | last post by:
Hello, When i read the CLI spec, 8.10.2 Method inheritance i read the following: "A derived object type inherits all of the instance and virtual methods of its base object type. It does not inherit constructors or static methods...." In the C# spec, 17.2.1 Inheritance i read the following:
5
1728
by: pgrazaitis | last post by:
I cant seem to get my head wrapped around this issue, I have myself so twisted now there maybe no issue! Ok so I designed a class X that has a few members, and for arguments sake one of the members Y is the location of a file to be read. The original design assumes that this class will be instantiated and each instance will happily mange its own members. (ie One file location per instance...no thread-safety). Now another class A...
0
8233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8170
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8675
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8619
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8334
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6108
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2604
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.