8

Quick and stupid question. How do I declare the size for an array if I'm not allowed to use global variables?

Suppose I have the file Album.h:

class Album {
private:
    Song songs[MAX_SONGS];

    //...

}

where do I put MAX_SONGS = 30? const int MAX_SONGS = 30 is considered a variable right? Please notice that the size should be known to the entire program.

4
  • 2
    const int MAX_SONGS = 30; is considered to be a constant. You could also use a #define MAX_SONGS 30 pre-processor directive. Commented Jan 11, 2012 at 21:20
  • 1
    Is there a reason for not using a container? Commented Jan 11, 2012 at 21:20
  • 2
    Use a std::vector<Song> and you don't have to define a max size. Commented Jan 11, 2012 at 21:21
  • 2
    I think there is no point in using a vector if the specs say MAX_SONGS must be 30. I'd prefer to use an identifier instead of writing 30 for code readability. Commented Jan 11, 2012 at 21:33

4 Answers 4

11
class Album {
private:
    static const int MAX_SONGS = 100;
    Song songs[MAX_SONGS];

    //...
};

Note that inline initialization of static const variables is only allowed for those whose type is integral. Also note that regardless of initialization, this is still just a declaration and not the definition. You won't generally need the definition, although there are certain cases when you would.

As for the visibility, you can provide a static getter function that would return MAX_SONGS.

public:
static int GetMaxSongs() { return MAX_SONGS; }
Sign up to request clarification or add additional context in comments.

3 Comments

MAX_SONGS must be public, per OP's requirement.
What about std::array<Song, MAX_SONGS> songs; -- then you can use songs.size() for querying, at no cost.
@KerrekSB: You'd still need a getter for the array itself, so it wouldn't be "at no cost".
4

const int MAX_SONGS = 30 is considered a variable right?

Yes, MAX_SONGS is a variable, but it is a constant variable. It can't change.

It's not so much that global variables are inadvisable, it's that global mutable state is inadvisable, if it can be avoided. There is no mutable state here: MAX_SONGS cannot change.

9 Comments

@PaulManta: Yes. That is, in fact, what it is.
The more important point is that MAX_SONGS is a constant expression. Consider an alternative extern const int MAX_SONGS; with an initializer in some other TU would not be a constant expression even if it is a constant variable.
I think the best way to do it is Armen's answer. What's the point of having the constant in every instance of the class? Wouldn't that bloat memory?
@JamesMcNellis 'Constant variable' makes no sense. Maybe 'constant object'? Or perhaps 'constant instance' is more C++-ish.
@PaulManta: MAX_SONGS is an object and because it has a name it is a variable. It is therefore a global variable and because it is const-qualified (and because, as Charles points out, it is initialized in its declaration) it is a constant. It is, in fact, a constant variable. @LuchianGrigore: The OP implied that he intended to declare MAX_SONGS either at namespace scope or as a static class member (otherwise, there's no way to consider it a global variable at all).
|
1

You could also use the #define preprocessor comand.

in Album.h

#define MAX_SONGS 30

in Album.cpp

#include "Album.h"
class Album {
private:
    Song songs[MAX_SONGS];
//...
}

2 Comments

Is the #define visible in other modules?
For that they must include Album.h. You could also make a Contants.h file and import that in Album.h and wherever it's needed
1

To avoid static usage, use enum:

class Album {
    private:
        enum { MAX_SONGS = 30 };
        Song songs[MAX_SONGS];

        //...
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.