-
November 2nd, 2010, 02:29 PM
#1
How to declare global variable across multiple files?
Hi,
I have multiple variables for which ones I have defaults. In java I would declare a static class like this
Code:
package com.xxxx.aaaa;
public class Constants {
public final static int a = 1;
public final static int b = 2;
}
Now I can access the variable in all source files via Constants.a
How would this look likein C++? In a header file I cannot initialize a variable. So even it is static I would have to create an instance and set all the member variables in the beginning. In the next souce file I would have to create again an instance, does this instance have the values automatically? Is that the way to go? What would be best?
Thanks,
J.
-
November 2nd, 2010, 02:38 PM
#2
Re: How to declare global variable across multiple files?
You can write:
Code:
class Constants {
public:
static const int a = 1;
static const int b = 2;
};
But it is something of a special case for static const member variables of integer types. Normally, you would write:
Code:
class Constants {
public:
static const double c;
};
then in a source file, you would write:
Code:
const double Constants::c = 1.5;
-
November 2nd, 2010, 02:55 PM
#3
Re: How to declare global variable across multiple files?
but do I have to write in all 5 source files again and again
Code:
const double Constants::c = 1.5;
?
I want to avoid is, if I have to change that default in the future I would have to search all files where I have set this value. I want one central place where to do it.
thanks, j.
-
November 2nd, 2010, 02:58 PM
#4
Re: How to declare global variable across multiple files?
Originally Posted by joebar
but do I have to write in all 5 source files again and again
No, you do that in exactly one source file, otherwise you would be breaking the one definition rule.
-
November 2nd, 2010, 02:59 PM
#5
Re: How to declare global variable across multiple files?
Originally Posted by joebar
but do I have to write in all 5 source files again and again
No. The proposed solution is more or less equivalent to the "C" approach of declaring the global "extern" in a header and then defining it in exactly one source file, except that it wraps this in a class.
-
November 2nd, 2010, 03:16 PM
#6
Re: How to declare global variable across multiple files?
I have
defaults.h
Code:
#ifndef DEFAULTS_H
#define DEFAULTS_H
using namespace std;
class Defaults
{
public:
static const double c;
};
#endif
And in some cpp file I have
Code:
#include "defaults.h"
using namespace std;
//some more code here
const double Defaults::c = 1.4;
During a "make" I get
invalid use of qualified-name ‘Defaults::c’
What is wrong?
-
November 2nd, 2010, 03:19 PM
#7
Re: How to declare global variable across multiple files?
It looks fine to me. What is the code that you commented out?
By the way, you should not have that using directive in the header file, at least not at file scope.
-
November 2nd, 2010, 03:35 PM
#8
Re: How to declare global variable across multiple files?
okay, crafted something really simple, luckily got the same error:
Code:
#include "mainwindow.h"
#include "defaults.h"
int main (int argc, char *argv[])
{
const double Defaults::c = 1.4;
MainWindow* window = new MainWindow();
}
MainWindow is an empty class with an empty constructor.
-
November 2nd, 2010, 03:36 PM
#9
Re: How to declare global variable across multiple files?
You should be defining that variable at file scope, not within a function.
-
November 2nd, 2010, 03:39 PM
#10
Re: How to declare global variable across multiple files?
Thanks, solved. Gretings to my old home S'pore.
-
November 2nd, 2010, 03:45 PM
#11
Re: How to declare global variable across multiple files?
Originally Posted by joebar
Code:
MainWindow* window = new MainWindow();
}
MainWindow is an empty class with an empty constructor.
You should never allocate using new when you don't have to. You certainly don't if MainWindow is as you say. If you choose to allocate using new, you should always pair it with a delete----even in a trivial example program. There's no reason to get into the bad habit of not matching those up every time!
-
November 3rd, 2010, 12:16 AM
#12
Re: How to declare global variable across multiple files?
Originally Posted by joebar
Code:
#include "mainwindow.h"
#include "defaults.h"
int main (int argc, char *argv[])
{
const double Defaults::c = 1.4;
MainWindow* window = new MainWindow();
}
MainWindow is an empty class with an empty constructor.
C++ isn't Java. The operator new in C++ is not the same as "new" in Java.
Don't ever believe that they are the same, as that will cause all sorts of grief in your C++ program.
Code:
int main (int argc, char *argv[])
{
MainWindow window;
}
This created an object, without the memory leak.
But basically, do not code C++ as if you're coding Java. Pretend that Java doesn't exist when you're writing a C++ program -- if you try to mimic Java in C++, you'll wind up with all sorts of problems.
Regards,
Paul McKenzie
-
November 3rd, 2010, 01:15 AM
#13
Re: How to declare global variable across multiple files?
Java has got a garbage collector , so any object referenced using new keyword will get reallocated automatically , with C++ you have to use delete keyword to free up the heap memory. Java borrows heavely from C++ not the other way round.
-
November 3rd, 2010, 03:45 AM
#14
Re: How to declare global variable across multiple files?
C++ has deterministic destructors which Java chose not to emulate. This gives an advantage to C++ when it comes to low memory usage and why Java, with its garbage collector can be a memory hog.
It just a case of Swings and roundabouts.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|