
Newbie: class available globally?
i have read acouple of posts regarding this but can''t seem to fully understand what is going on and what i need to do.
What i want is to have my class available to all my separate cpp files like main.cpp and menu.cpp etc, so that i can call the class in both main.cpp and menu.cpp.
It appears that i need to use #indef and #define etc but i don''t understand what i need to do to get my class as globally available to all my files in my program.
From what i can tell i would put the class into its own separate cpp file, and using #define etc make it available to all my other files, i just don''t get it though.
Can anyone explain it to me in plain english please??:D lol
Thanks for your time
Werdy666

/* Class.h file*/class OurClass{public: int Number;public: OurClass();};extern OurClass AClass;/* End of Class.h file*//* Class.cpp file*/OurClass::OurClass(){ Number=0;}OurClas AClass;/* End of Class.cpp file*/
Now in all your source files, just #include "Class.h". The extern tells all your files that there is an "external" variable called AClass (which there is in our Class.cpp file), so they can now all see it.
Billy - BillyB@mrsnj.com
(Hey, I''''m no longer anonymous!)
Hi
You could do something like this.
#ifndef __SOMECLASS_H__
#define __SOMECLASS_H__
class SomeClass
{
public:
SomeClass();
~SomeClass();
};
#endif
HTH
/Niklas
You could do something like this.
#ifndef __SOMECLASS_H__
#define __SOMECLASS_H__
class SomeClass
{
public:
SomeClass();
~SomeClass();
};
#endif
HTH
/Niklas
thanks for your replies!
Ready4Dis : your suggestion i had absolutely no luck with sorry!
Niklas) : I had better luck with yours although it still won''t compile.
If anyone would be willing to look at my code and explain to me what i have done wrong and any tips on how to improve it, I would be very grateful! just email me and i will send u the code. (3k zipped).
thanks for your time!
Werdy666
Ready4Dis : your suggestion i had absolutely no luck with sorry!
Niklas) : I had better luck with yours although it still won''t compile.
If anyone would be willing to look at my code and explain to me what i have done wrong and any tips on how to improve it, I would be very grateful! just email me and i will send u the code. (3k zipped).
thanks for your time!
Werdy666

Ready4Dis is basically correct, but you may have misunderstood his solution...
In your header file, you will need the line:
extern MyClass g_myClass;
or something like it. The extern keyword tells the compiler that an object g_myClass exists, so you are able to use it, but it does not actually create the object. So if that is all you do, it will compile, but the linker will fail.
In order to get it to link, you have to declare the global properly. In one (and only one) of your .cpp files, add the line
MyClass g_myClass;
This will create g_myClass in the object file which gets created when you compile that .cpp, so you when you link the files the g_myClass object will actually exist.
NiklasO's answer is not strictly relevent to global variables, but inclusion guards are good coding practice, and you should use them in all of your headers.
[edited by - Sandman on May 14, 2002 11:21:21 AM]
In your header file, you will need the line:
extern MyClass g_myClass;
or something like it. The extern keyword tells the compiler that an object g_myClass exists, so you are able to use it, but it does not actually create the object. So if that is all you do, it will compile, but the linker will fail.
In order to get it to link, you have to declare the global properly. In one (and only one) of your .cpp files, add the line
MyClass g_myClass;
This will create g_myClass in the object file which gets created when you compile that .cpp, so you when you link the files the g_myClass object will actually exist.
NiklasO's answer is not strictly relevent to global variables, but inclusion guards are good coding practice, and you should use them in all of your headers.
[edited by - Sandman on May 14, 2002 11:21:21 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement