Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   C++: How to add const variables, structures, and arrays to classes (http://www.go4expert.com/forums/cpp-add-const-variables-structures-t28228/)

kris10 19Apr2012 10:41

C++: How to add const variables, structures, and arrays to classes
 
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?

xpi0t0s 20Apr2012 11:48

Re: C++: How to add const variables, structures, and arrays to classes
 
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.


All times are GMT +5.5. The time now is 02:18.