S
subramanian100in
I read that we can define a const variable in a header file, only if
the variable is initialized by a constant expression that can be
evaluated at compile-time. For example, we can have the following
header file:
File Sample.h:
---------------------
#ifndef _SAMPLE_H
#define _SAMPLE_H
const double d = 1.1;
#endif
But we can also initialize a const variable in a source file only if
it is qualified by extern specifier, in order to be used in other
source files(I understand the reason for specifying extern for const
variable initializations in source files). For example, consider the
program consisting of var_declr.h, x.cpp and y.cpp:
File var_declr.h:
-----------------------
#ifndef _VAR_DECLR_H
#define _VAR_DECLR_H
extern const double d;
extern void fn(void);
#endif
File x.cpp:
---------------
#include <cstdlib>
#include <iostream>
#include "var_declr.h"
using namespace std;
extern const double d = 1.1;
int main()
{
cout << "from main: " << d << endl;
fn();
return EXIT_SUCCESS;
}
File y.cpp:
---------------
#include <iostream>
#include "var_declr.h"
using namespace std;
void fn(void)
{
cout << "from fn(): " << d << endl;
return;
}
Thus we can define a const variable intialized by a constant
expression that can be evaluated at compile-time, in both header file
as well as source file. If we want to make any change to the
initializing expression, we can do so both in header and source files.
There is no difference in making changes to header or source files.
My question: What are the advantages of initializing a const variable
in a header file over initializing it in a source file ?
Kindly explain.
Thanks
V.Subramanian
the variable is initialized by a constant expression that can be
evaluated at compile-time. For example, we can have the following
header file:
File Sample.h:
---------------------
#ifndef _SAMPLE_H
#define _SAMPLE_H
const double d = 1.1;
#endif
But we can also initialize a const variable in a source file only if
it is qualified by extern specifier, in order to be used in other
source files(I understand the reason for specifying extern for const
variable initializations in source files). For example, consider the
program consisting of var_declr.h, x.cpp and y.cpp:
File var_declr.h:
-----------------------
#ifndef _VAR_DECLR_H
#define _VAR_DECLR_H
extern const double d;
extern void fn(void);
#endif
File x.cpp:
---------------
#include <cstdlib>
#include <iostream>
#include "var_declr.h"
using namespace std;
extern const double d = 1.1;
int main()
{
cout << "from main: " << d << endl;
fn();
return EXIT_SUCCESS;
}
File y.cpp:
---------------
#include <iostream>
#include "var_declr.h"
using namespace std;
void fn(void)
{
cout << "from fn(): " << d << endl;
return;
}
Thus we can define a const variable intialized by a constant
expression that can be evaluated at compile-time, in both header file
as well as source file. If we want to make any change to the
initializing expression, we can do so both in header and source files.
There is no difference in making changes to header or source files.
My question: What are the advantages of initializing a const variable
in a header file over initializing it in a source file ?
Kindly explain.
Thanks
V.Subramanian