Hello. A question. You need to create a global array of elements of type <MyClass> so that it is available in all functions of the program. Is there such a possibility?

class declaration:

value class card { public: int suit,number; }; 

I create an array:

 array < card^ >^ mass= gcnew array <card^ >(9); for (int i=0;i<9;i++) { mass[i]=gcnew card; mass[i]->suit=0; mass[i]->number=0; } 

Where to put this ad, or how?

  • Do this: card a [54]; - sudo97
  • Ilya, you should know that this way you cannot create an array of this type in vc ++. - Vasily Koshelev

4 answers 4

I do not know the CLI, I can give a general sentence, based on the knowledge of other languages. Maybe you should declare a global pointer to the desired array (if possible in the CLI)? And quickly create an array and initialize this pointer. Can you make extern card * variables in this language?

  • Yes, there is such an opportunity. Thank you very much, I will try to implement this method! - Vasily Koshelev

Are you sure that you need to use C ++ CLI here? There are some problems with global controlled variables. In ordinary C ++, creating and initializing an array can be written much easier:

 class card { public: int suit, number; }; card mass[9]={0}; 

If you need to create it in dynamic memory, then you must either make a default null constructor, or reset it yourself in a loop.

Generally, globals are a bad thing. Using them is a bad programming tone. It is better to use only global constants.

Reply to the first comment:

So far, not quite imagine how to write applications without using global variables.

You must pass variables to functions or use classes. For example, it is often possible to logically combine into a class one or more global variables and functions that work with them.

The fact of the matter is that I pretty well know the usual C ++, and when switching to Visual C ++ I encountered such a problem.

It seems to me that you confuse the terms. Visual C ++ is just a development environment. C++\CLI you use is an extension of the Microsoft language. It can not be used and written in ordinary C ++. You can easily mix regular and advanced C ++. Then you can make this class as before, and where it gives an advantage, use advanced features. In fact, C++\CLI intended only for communicating unmanaged code with managed code, which, for example, is written in C # or VB .NET. Simply writing programs on it is not recommended.

I'm used to writing console applications, but everything is simple, exactly as you wrote.

That is, do you think that using C++\CLI with Windows Forms is the only way to write window applications in C ++ ?! For regular C ++, there are a bunch of libraries, for example, MFC, QT. Finally, you can write on a pure Windows API by connecting <windows.h>. I believe that window applications are best written on C # with Windows Forms. C # is, by its capabilities, equivalent to C++\CLI extensions, but its syntax is much simpler and more similar to regular C ++.

  • one
    The fact of the matter is that I pretty well know the usual C ++, and when switching to Visual C ++ I encountered such a problem. I'm used to writing console applications, but everything is simple, exactly as you wrote. So far, not quite imagine how to write applications without using global variables. It seems that much will have to revise in their habits. Thanks for the help, but the problem remained unsolved. - Vasily Koshelev
  • Replied to your comment by updating the answer. - gammaker
  • Sorry, did not say so. I know that Visual c ++ is not a separate language, but a programming environment. And I also use regular C ++ in a Visual environment, which is clearly seen in the code. I would like to write applications in C ++ / CLI, since I have been doing C ++ for a relatively long time. But I can’t imagine how to write applications with MFC, QT. Again, I don’t want to switch to Sharp, because, like it or not, you’ll have to study it from scratch. Thanks for the explanation. But nevertheless, by what means can I realize my initiative to code? Do not want to rush from one extreme to another. - Vasily Koshelev
  • > But nevertheless, by what means can I realize my initiative to code? Do not want to rush from one extreme to another. Use the fact that you can mix regular C ++ with extensions. This part of the code above can be rewritten into ordinary C ++, and let work with the GUI windows be written in C++\CLI . > Again, I do not want to go to Sharp, because, anyway, but you have to study it from scratch. It can be studied in a week, if you already know C ++ and the CLI extension. - gammaker
  • Thank you, I will continue to understand! Here I will deal with the pluses, and there already I will try Sharp necessarily). - Vasily Koshelev

I will say as a .net programmer. I have never met C ++ / CLI on real projects, it is cumbersome and not convenient. Approximately 70% of .net applications are written in C #, 25% are VB, <5% are IronPython. So think about whether it makes sense to understand it at all. I think C # is a simpler, more convenient and promising language.

  • In general, the farther from Bill Gates, the better. - avp
 int[] p = new int[3] = { 1,2,3 }; //это я создал массив с тремя полями типа инт for(int i=0; i < 3; i++) { cout << p[i]; } 

You can call like this:

  • What kind of language is that? This is int [] p = new int [3] = {1,2,3}; more like C #, but in C # you don't need to put an = sign when initializing the selected array. cout << p [i]; And this is clearly C ++. > You can call it like this: Unexpected end of file. - gammaker
  • Yes, screwed up. int * array = new int * [row_count]; for (int r = 0; r <row_count; r ++) array [r] = new int [column_count]; - dajver
  • Sorry, do you see the question at all? I did not see here either classes or an array, which will be of the “class” type and at the same time all functions of the program will have access to it, i.e. array must be of global type. And I know that information that you wrote so well, for I have worked with C ++ for over a year. - Vasily Koshelev