Modifying a Constant Global Variable??

Hi,

#I am making an audio player in CMD.

#I want to modify the song's listSize "const int listSize" which is global to all functions.

#I am using a function to read the songs from a text file to determine the number of records in the file, but I don't know how to pass its value to the listSize variable so that it can be modified dynamically.

P.S.: I am still Beginner

| Thanks in advance for any idea
| Mubarak
well I#d say a constant is just that not changeable. The compiler already knows the value and might do optimisation, no change during runtime.

Anyway it's not good setting up global variables. (constants might be alright)

I would go for some class "audioPlayer" within which you set up your list size. All member functions are able to use the value and if not set constant its even changeable. You'll want to declare two functions: getListSize() and setListSize( int newSize) those fuctions can perform any necessary steps resetting other affected functions etc.
Last edited on
BTW: nice nickname...looking for some new job after quitting politics ;)
I do not know how to use class, but I'll try.
Thanks :) ZED0815
did you use structures before? a class is prettymuch the same (even if some real programmers will punch me for this statement)

you have some contained data, as long as you are new to this stuff have it the easy way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class myClassName //whatever Name
{
     private:
         //all contained data, might be any object even a pointer to the class itself:
         double someValue;
         char * someBuffer //while setting the whole thingy up you might not know how big your arrays will become, make it dynamical!
         //other stuff you might need...

    public:
        myClassName(); // this is a function template for very important one: the constructor...if you create a new object of type myClassName in your programm, this function will run automatically, use it to initialize your variable, e.g. array size via "new" directive

        ~myClassName(); //destructor: deallocate any memory, you used. Your char* has got some allocated memory for sure use "delete" to free it

        //other member functions, these are the only functions (for now) which can access your private data. So you'll need some functions just returning the value of the data, and some setting new values. The benefit is: you controll very exactly how your data is been set and which other stuff might not be valid anymore...you can do some safety stuff as well without the need of rewriting it everytime you set a new value, e.g. make sure your array index is valid or stuff like that.

}; //well thats pretty much it, a class, some data, some function, packed together: a new datatype. 

Use it like some integer or stuff:

int temporary; // alocates some memory named temporary from now on
MyClassName temporary; // well pretty much the same, runs your contructor as well

be aware of c++ benefits: overloading memberfunctions as well as contructors - giving them default values or stuff like that makes it very usefull

you can't overload destructors though there are always ~className( void );

Last edited on
When getting used to it, there are other ways to do it, data does not to be private for example (it just might be safer keeping it this way..)
thanks alot for this compact example.

it's time to play with "Classes"
Topic archived. No new replies allowed.