473,698 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++/CLI: Declaring a constant array

I'm new to C++/CLI and my main background is Java. I'm trying to
create an array of constants as a static member of a class. An
example is shown below:

public ref class TestClass {
public:
static const array<constant int>^ stuff = {1,2,3,4,5};
};

This gives me compiler error C4538 which basically says that you can't
declare an array as constant. So that means that the values in my
array can't be changed, but nothing stops someone from assigning a new
array to the "stuff" variable. Am I missing something or is it not
possible to create a fixed array of constants?
Jun 27 '08 #1
3 4055
lo******@gmail. com wrote:
public ref class TestClass {
public:
static const array<constant int>^ stuff = {1,2,3,4,5};
};

This gives me compiler error C4538 which basically says that you can't
declare an array as constant.
Try `literal' instead of `static const'.

Tom
Jun 27 '08 #2
lo******@gmail. com wrote:
I'm new to C++/CLI and my main background is Java. I'm trying to
create an array of constants as a static member of a class. An
example is shown below:

public ref class TestClass {
public:
static const array<constant int>^ stuff = {1,2,3,4,5};
};

This gives me compiler error C4538 which basically says that you can't
declare an array as constant. So that means that the values in my
array can't be changed, but nothing stops someone from assigning a new
array to the "stuff" variable. Am I missing something or is it not
possible to create a fixed array of constants?
Use the "initonly" keyword. Note that "constant int" won't compile, you
probably meant "const int". In general C++/CLI support for
const-correctness is very poor.
Jun 27 '08 #3
On May 23, 9:27 am, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
>
Use the "initonly" keyword. Note that "constant int" won't compile, you
probably meant "const int". In general C++/CLI support for
const-correctness is very poor.
I ended up figuring that out for myself. The solution seems to work
fine for C++/CLI, but not for the .NET languages in general. I think
if you were to create a class like this and use it in C#, you would be
restricted from assigning the variable to a new array, but nothing
will stop you from modifying the values in the array itself. I
haven't actually tried this, but I have created an equivalent class in
C# and it does allow reassigning the array values. The only
difference here is that you can't specify the datatype as a "const
int" in C# because the const keyword conflicts with the readonly
keyword.

So I guess I need to come up with a better idea of how I want to
organize these constant values.
Jun 27 '08 #4

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

Similar topics

14
74868
by: Eric Bantock | last post by:
Very basic question I'm afraid. Once an array has been declared, is there a less tedious way of assigning values to its members than the following: myarray=8; myarray=3; myarray=4; myarray=0; myarray=0; myarray=1; myarray=8;
6
2063
by: JNY | last post by:
Hello, Is it possible to declare an array with variable indeces? i.e. int x = 4; int myArray; for (j = 0;j < 5;j++) {
2
7756
by: None | last post by:
Hello To get an understanding of UI threads in C++/CLI, I attempted to duplicate the following C# example: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp Here is the code: public ref class Form1 : public System::Windows::Forms::Form
0
1125
by: none | last post by:
Hello I have created a realtime data intensive application in C++/CLI. Data streams into this application through a set of ActiveX controls. For example, each stock symbol will be assigned to one of these controls and the symbol will be updated through one of the events of these controls. Symbols and their corresponding data are displayed in a listview. Data is updated few times a second per symbol.
3
2223
by: farseer | last post by:
i am getting "error C2057: expected constant expression" with the following code: ifstream f( argv ); f.seekg( 0, ios::end ); const long fSize = f.tellg(); f.close(); char content;
6
15667
by: DaTurk | last post by:
I'm converting MC++ to C++/CLI and for some reason it's not cool with static const System::String^ How am I supposed to have static constant strings then?
1
2463
by: vijay.gandhi | last post by:
Hello, I have created a function in C++/CLI which was exported as a .DLL to be used in VB .NET. I have been having some problems (I think it has to do with the right syntax) with parameter passing across the languages. Can somebody help me please. Details are given below: The function ComputeMedian(x,y,m_x,m_y) is supposed to have two input parameters: x, y - both are array of double; and two output parameters: m_x, m_y - both are...
11
4651
by: memeticvirus | last post by:
I have an array cli::array<float, 2and I would like to access a subset of it's values by compiling an array of pointers. But, it's not possible to create an array of type cli:array<cli::interior_ptr<float>, 2>... So, what do I do?
2
3045
by: Pavan | last post by:
Hi, I need to create a consant array of larze size but however its elements can be calculated using an equation, say for example I need an int arry of 20 elements where each element will be arr = 2 + (i*i) But I want arry to be constant. How can I declare such a constant array without actually defining all the elements?
0
9030
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
8899
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,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6528
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
5861
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();...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.