Accessing variables declared in Main

Hello,

i have this variable in the main.cpp file:

1
2
3
4
//Screen dimension constants
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 768;
int scrollingYOffset=0;

Now, having created a class that need to know the SCREEN_WIDTH as follow:

 
    if( ( mPosX < 0 ) || ( mPosX + DOT_WIDTH > SCREEN_WIDTH ) )


I get his error trying to get the SCREEN_WIDTH
error: 'SCREEN_WIDTH' was not declared in this scope|

How can access this variable SCREEN_WIDTH from within a class?

I know that global variables should be avoided.

Any tips?

Thank you
Last edited on
since it is constant you can move it out to the global scope as a constant expression, or even into a header file.

Constants are NOT variables :)
Constants cannot change and while in large programs are probably best put into a namespace (and at global scope), for smaller programs global access is fine without the namespace. Variables can change, and are not so hot at the global scope.

it also can be determined by where you need it. If the whole program needs it, that is global, but if only one area needs it, maybe it belongs in the class header, or a local cpp file scope, .... get it where it needs to go, and try to limit it so it goes no farther than that, as programs get bigger this becomes more and more of a good thing to do.
Last edited on
Thank you.

I remember opened a thread 3 years ago with almost the same argument.
"Constants are NOT variables :) "
I know, sorry, but i mean both
SCREEN_WIDTH, SCREEN_HEIGHT and scrollingYOffset

If i create an header to hold few global var and constant is consider a bad practice?
Last edited on
variables globally are a bad practice, yes. Constants are fine; I have somewhere one with like 50 common math constants like pi, e, rad2deg, etc...

can you make the y-offset a static class member? Then if it changes in one, it changes for all objects of that class. If its needed by other classes, you can do something else ... how far reaching is it? Can you pass it into constructors? Should to take a pointer or reference to it? Does it need to be thread safe?
> Constants are NOT variables

In const int SCREEN_WIDTH = 1024; SCREEN_WIDTH is a variable of type const int.
Last edited on
Yes, if you are a compiler, and it should have been a constant expression instead, but that aside... the issues from global varying data items are caused by the ability to write to them. I am unaware of any problems caused by reading an item that cannot (or, for that matter, simply does not) change.


> and it should have been a constant expression instead

SCREEN_WIDTH already is a glvalue core constant expression

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

const int SCREEN_WIDTH = 1024;

constexpr int TWICE_SCREEN_WIDTH = SCREEN_WIDTH * 2 ;
static_assert( TWICE_SCREEN_WIDTH == (SCREEN_WIDTH+SCREEN_WIDTH), "" ) ;

int main( int argc, char*[] )
{
    switch(argc)
    {
        // addressof operator is allowed because SCREEN_WIDTH is an lvalue
        // *( &SCREEN_WIDTH ) is a constant expression because SCREEN_WIDTH is a glvalue core constant expression
        case *( &SCREEN_WIDTH ) / 1024 :
            std::cout << "argc == " << sizeof( char[*(&SCREEN_WIDTH)/1024] )
                      << "  ie. argc == " << std::integral_constant< int, *(&SCREEN_WIDTH)/1024 >::value << '\n' ;
            break ;

        default: std::cout << "argc != 1\n" ;
    }
}

http://coliru.stacked-crooked.com/a/a9f80ed92ea84f67
http://rextester.com/HIFMX9330
Create a header which defines these compile time constants. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
// screen_geometry.h

#ifndef SCREEN_GEOMETRY_H_INCLUDED
#define SCREEN_GEOMETRY_H_INCLUDED

namespace screen
{
    static const int WIDTH = 640;  // C++11: we can use constexpr instead of const
    static const int HEIGHT = 480; // eg, constexpr int HEIGHT = 480;
    static const int scrollingYOffset=0;
}

#endif // SCREEN_GEOMETRY_H_INCLUDED 


Include this header in the header for the two classes. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Ltexture.h

#ifndef LTEXTURE_H_INCLUDED
#define LTEXTURE_H_INCLUDED

#include "screen_geometry.h"

class LTexture
{
    // use screen::WIDTH instead of SCREEN_WIDTH
    // use screen::HEIGHT instead of SCREEN_HEIGHT
    // use screen::scrollingYOffset instead of scrollingYOffset
};

#define LTEXTURE_H_INCLUDED 


And likewise, for the other class.
Last edited on
Thank you for the precious info.
Topic archived. No new replies allowed.