C++: How to add const variables, structures, and arrays to classes

Discussion in 'C++' started by kris10, Apr 19, 2012.

  1. kris10

    kris10 New Member

    Joined:
    Apr 19, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Alright I am new to c++ so this may be fairly obvious, but bear with me.

    I have to define structures within a class, and arrays with constant variables as arguments. I have something like this so far:

    Code:
    class class_name
    {
    public:
        struct struct_name
            {
                int myArray[CONST_VARIABLE];
             };
    
    in my .h file
    Questions:

    • Where do I declare the constant variables such that they can be used in the main( ) function, and each object of my class_name?
    • How/where do I declare an instance of struct_name such that I can access and input the different parts in a function in my main.cpp file?
    • Can I declare an array with a variable in the [] that is not constant? (I don't know what [] is called) In other words, can I have arrayName[variable] where the value of [variable] changes throughout the program?
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    1. The only rule is that you must define it before it is used. So that can be on the line immediately before the myArray declaration, or before the class class_name, or in a different header that is #included by this header or your C file, or #included by another header that itself is #included from another header or your C file, or in your main C file before you #include this header.

    2. You need additional lines of the form "struct struct_name variable_name", then if you have class_name X, you can reference myArray with X.variable_name.myArray. struct struct_name { /* contents */ }; only defines a new type struct_name, it does not declare any variables.

    3. No, array dimensions must be constant. If you need variable size arrays whose size is not known until runtime (and you don't want to use a fixed size array with a count of how many items are in it, and of course an upper limit of how many items you can stuff into it), you need to use pointers and new/malloc, which sounds complicated but in reality is very easy, For example:

    Code:
    int *dynArray=0;
    int arrsize;
    cout<<"Enter size : ";
    cin>>arrsize;
    dynArray=new int[arrsize];
    
    // you can now access dynArray with static array syntax, for example:
    
    dynArray[5]=27; // assuming arrsize>5, of course, so always check you don't write to memory after arrsize unless you want your program to suffer from some really nasty bugs
    
    cout<<"dynArray[5] contains " << dynArry[5] << endl;
    
    // after you've finished with dynArray, do this to prevent memory leaks:
    delete[] dynArray;
    dynArray=0;
    
    Initialising dynArray to zero and setting it to zero after delete[]ing it is a good way of avoiding some very nasty bugs.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice