Which of these classes does what I want??

Here I have two classes:

const int ADDITION = 1;
const int SUBTRACTION = 2;
const int MULTIPLICATION = 3;
const int DIVISION = 4;

public value class CalculationOperators
{
public:
static array<const int>^ calculationOperatorList = {ADDITION,SUBTRACTION,MULTIPLICATION,DIVISION};
};

public value class CalculationOperators
{
private:
array<const int>^ col;
public:
property const array<const int>^ calculationOperatorList
{
array<const int>^ get(void)
{
this->col = gcnew array<const int>{ADDITION,SUBTRACTION,MULTIPLICATION,DIVISION};
return this->col;
}
}
};

Now I wanted a const array with values you se at the top but I could not figure out how to create that array. So I ended up creating two value classes instead. I used value because I wanted it to be created on the stack. I am only going to create objects of the class locally in functions, and I want it to be fast and not consuming a lot of memory. The second one should be better because it initiates it's array only when you use the property. The first one gets it's public variable initiated from start whitout even Initiating a object of the class just because it is static.

Which one does the job best and can I do it in a better way

Do I have right in my conclusion about how it works




Answer this question

Which of these classes does what I want??

  • Helen999888

     Andreas Asterlund wrote:
    static array<const int>^ calculationOperatorList = {ADDITION,SUBTRACTION,MULTIPLICATION,DIVISION};
    That is a pointer to an array, not an array, correct The values you provide would not be used, even if there was no other problem there.

    Also, I think that you want to use "static const" here; I think that that is the only syntax that allows a member variable to be assigned a value.

    Also, I am not sure that the syntax you are using can be used to supply values; that is, values separated by commas in braces. That works for non-STL arrays but I am not sure it works for STL arrays. So if it does not work when you try it, then you probably need to figure out how to do it in a more complicated way.

     Andreas Asterlund wrote:
    I am only going to create objects of the class locally in functions, and I want it to be fast and not consuming a lot of memory.
    Creating on the stack will consume valuable stack space. You should avoid using stack space for large amounts of data. The best reason is that stack space must be contiguous, so use of large amounts can be more of a problem than use of heap space.

    If however an item is static, then by definition stack space is not used.



  • Participant

    Samuel Hobbs wrote:

    Andreas Asterlund wrote:
    static array<const int>^ calculationOperatorList = {ADDITION,SUBTRACTION,MULTIPLICATION,DIVISION};

    That is a pointer to an array, not an array, correct The values you provide would not be used, even if there was no other problem there.

    Also, I think that you want to use "static const" here; I think that that is the only syntax that allows a member variable to be assigned a value.

    I have tested both classes and they seem to work just fine.

    Samuel Hobbs wrote:
    Also, I am not sure that the syntax you are using can be used to supply values; that is, values separated by commas in braces. That works for non-STL arrays but I am not sure it works for STL arrays. So if it does not work when you try it, then you probably need to figure out how to do it in a more complicated way.

    Do I have to wory about STL/non-STL when working with managed C++

    Samuel Hobbs wrote:

    Andreas Asterlund wrote:
    I am only going to create objects of the class locally in functions, and I want it to be fast and not consuming a lot of memory.

    Creating on the stack will consume valuable stack space. You should avoid using stack space for large amounts of data. The best reason is that stack space must be contiguous, so use of large amounts can be more of a problem than use of heap space.

    If however an item is static, then by definition stack space is not used.

    I was aming to develop a small class that don't comprise large amounts of data. In your opinion, have I missed my goal

    duck thing wrote:

    Could you give a bit more information about what you're trying to do here I'm confused as to what you think this code should be doing. Is this a homework assignment

    No it's not an assignment. It is just an array with constants used in a small application that I am developing. The application is used for developing your calculation skills. Just a small app (addition, subtraction, division, multiplication and mixed).

    Damien Watkins - MSFT wrote:

    Hello

    I assume here you want a C++/CLI answer (you use ^ in your code.)

    So with a question like this I always see what the experts do. If I look at the BCL classes for something similar I could look at the FileAccess Enumeration (http://msdn2.microsoft.com/en-us/library/system.io.fileaccess.aspx). The documentation describes this as:

    public enum class FileAccess

    And I use it like this:

    FileStream^ s2 = gcnew FileStream( name, FileMode::Open, FileAccess::Read, FileShare::Read );

    I would model your solution more like this – seems elegant and has a very BCL feeling which you want for C++/CLI code you may want to expose to other developers/languages.

    Thanks

    Damien

    Not quite right. But it's an elegant sulotion indeed!

    What do you mean by BCL and BCL feeling

    It all started when I had this headerfile with my constants and I wanted to make an array out of them. But I could not figure out how to declare other types than constants outside a class or function, so I ended upp creating a class for it. In my case an Enumeration would not pass the test because you can not use it like an array... or can you

    In the end I ended up reworking the system so the use of my classes was no longer needed.



  • Cerberuss

    Could you give a bit more information about what you're trying to do here I'm confused as to what you think this code should be doing. Is this a homework assignment



  • Andriy G.

    Hello

    I assume here you want a C++/CLI answer (you use ^ in your code.)

    So with a question like this I always see what the experts do. If I look at the BCL classes for something similar I could look at the FileAccess Enumeration (http://msdn2.microsoft.com/en-us/library/system.io.fileaccess.aspx). The documentation describes this as:

    public enum class FileAccess

    And I use it like this:

    FileStream^ s2 = gcnew FileStream( name, FileMode::Open, FileAccess::Read, FileShare::Read );

    I would model your solution more like this – seems elegant and has a very BCL feeling which you want for C++/CLI code you may want to expose to other developers/languages.

    Thanks

    Damien



  • Which of these classes does what I want??