Skip to main content
gamedev.net
🔒 Locked

variable scope (basic easy question)

Started by Scooter 2 replies 200+ views
Original Post
Scooter
Scooter
I have two .cpp files and in the first one there''s the : LPDIRECTDRAW7 g_ddraw = NULL; how can i access g_ddraw in the second .cpp file so i can call g_ddraw->GetDisplayMode() ? or do i have to pass the value as a parameter through every function till the one i want to use it in ?
We are here just like this,It really doesn't matter why.The world is here like it is,'We could laugh we could cry.Is it good or is it bad,Wright or maybe wrong?There's no use of being sad,Without a purpose within a song.
Sly
Sly
In the header for the first source file, add the line

extern LPDIRECTDRAW7 g_ddraw;

In the second .cpp file, include the first .h file.



Steve 'Sly' Williams
Tools Developer
Krome Studios
IOFILE
IOFILE
I agree completely with Sly. Create the the global in one cpp file and then refer to it with the keyword extern from an h file, and include that in all of the files that need to use the variable. For example:

dd.cpp

LPDIRECTDRAW7 g_ddraw = NULL;
...

dd.h

extern LPDIRECTDRAW7 g_ddraw;
...

ddsurface.cpp

#include "dd.h"
...
g_ddraw->...
...

hope the example helped
IOFILE
Scooter
Scooter
yeah that helps if they''re both .cpp files and no .h files i can just put:extern LPDIRECTDRAW7 g_ddraw; at the top the second .cpp file right ?
We are here just like this,It really doesn't matter why.The world is here like it is,'We could laugh we could cry.Is it good or is it bad,Wright or maybe wrong?There's no use of being sad,Without a purpose within a song.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.