Board index » cppbuilder » OOP and Global variables?

OOP and Global variables?


2007-04-16 09:22:10 PM
cppbuilder61
Hi all,
I was wondering was the normal method of defining global variables
would be using OOP principles in C++ Builder. The method I have used
is to create a class with public variables and then create an
instance
beneath the class e.g
class Contants{
....
};
Constants Settings
But i'm getting alot of linker warnings from all the files in the
project the compiler saying that "Settings is deifned elsewhere", can
anybody help?
Thanks
Daniel
 
 

Re:OOP and Global variables?

"Mr B" < XXXX@XXXXX.COM >wrote:
Quote
class Contants{
....
};
Constants Settings

But i'm getting alot of linker warnings from all the files in the
project the compiler saying that "Settings is deifned elsewhere", can
anybody help?
Ah, you have a problem there.
What you've done is define an instance Settings for each source file
that includes that header, rather than one for all of them.
Instead, do the following
class Constants
{
// ....
};
extern Constants Settings; // note the 'extern'
and in one, and only one, source file
Constants Settings;
which is the single one that all will share.
Alan Bellingham
--
10th Anniversary ACCU Conference: 11-14 April 2007 - Oxford, UK
(Booking now open: see accu.org/index.php/conferences for details)
 

Re:OOP and Global variables?

"Mr B" < XXXX@XXXXX.COM >writes:
Quote
Hi all,

I was wondering was the normal method of defining global variables
would be using OOP principles in C++ Builder.
Global variables are not object-oriented in any way. Objects
encapsulate and hide data, global variables are the exact opposite.
Your problem is not of OOP, but just how C++ declarations and linking
work.
Quote
The method I have used is to create a class with public variables
and then create an instance beneath the class e.g

class Contants{
....
};
Constants Settings
If you do this in a header, then Settings will be declared in every
file that includes that header. Obviously for a global you want
only one instance, so this is clearly a problem.
You should define Settings in one (and only one) .cpp file, by
removing the "Constants Settings" declaration from the header. Only
leave the class definition in the header.
--
Chris (TeamB);
 

{smallsort}

Re:OOP and Global variables?

Thanks for the response,
but i'm still having problems trying both of the ways suggested.
extern Constants Settings; //comes up with an unresolved linker error
for one of the files, strangely works for the others
//all files have the #include "Constants.h" header
and i also declared Constants Settings; //inside the main.cpp file,
but for some reason none of the other files seem to be able to find it
as it says typedef underfined.
All of the units have the #include "main.h", and #include
"constants.h" and all are added to the project (the application works,
this was an addition) does it matter where these include headers are
placed (units header or cpp)??
Is there something simple i've forgotten to do here?
Thanks alot
Daniel
 

Re:OOP and Global variables?

Mr B < XXXX@XXXXX.COM >wrote:
Quote
Thanks for the response,

but i'm still having problems trying both of the ways suggested.

extern Constants Settings; //comes up with an unresolved linker error
for one of the files, strangely works for the others
//all files have the #include "Constants.h" header
Read Alan's posting again. He wrote:
"and in one, and only one, source file
Constants Settings;
which is the single one that all will share."
It seems you missed that one.
Quote
[...]
Daniel
Schobi
--
XXXX@XXXXX.COM is never read
I'm HSchober at gmx dot org
"My hope is that if more people start reading books,
the world will become a better place."
froarulv in afp