473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

declaring char* in header file

1
Hi,

In C++, how can i declare a const char* in header file. Say, i have a header file MyTest.h as under:

MyTest.h

const char* temp = "Temp";

class MyClass
{
private:
public:
};
Jun 23 '10 #1

✓ answered by weaknessforcats

Do not define variables in header files. When you do you get a new variable every time you include that header. What you want is one variable fro the entire program.

Header files are for declarations only.

Define your const in an implementation file:

Expand|Select|Wrap|Line Numbers
  1. MyTestGlobals.cpp: 
  2.  
  3. extern const char* temp = "Temp";
This will create temp as a sharable const variable.

Then in your header file:

Expand|Select|Wrap|Line Numbers
  1. MyTestGlobals.h
  2.  
  3. extern const char* temp;
Finally, include your header:

#include MyTestGlobals.h

class MyClass
{
private:
public:
};

3 10647
weaknessforcats
9,208 Expert Mod 8TB
Do not define variables in header files. When you do you get a new variable every time you include that header. What you want is one variable fro the entire program.

Header files are for declarations only.

Define your const in an implementation file:

Expand|Select|Wrap|Line Numbers
  1. MyTestGlobals.cpp: 
  2.  
  3. extern const char* temp = "Temp";
This will create temp as a sharable const variable.

Then in your header file:

Expand|Select|Wrap|Line Numbers
  1. MyTestGlobals.h
  2.  
  3. extern const char* temp;
Finally, include your header:

#include MyTestGlobals.h

class MyClass
{
private:
public:
};
Jun 23 '10 #2
donbock
2,426 Expert 2GB
If you do have a good reason to define this variable in the header file (and there are very few good reasons), then that means that you are trying to define an initialized file-local variable in each source file that includes this header. You need to do this in the header:
Expand|Select|Wrap|Line Numbers
  1. static const char* const temp = "Temp";
  • static prevents duplicate definition errors at link time.
  • The first const insures that the string can't be changed.
  • The second const insures that the pointer can't be changed to point at some other string.
Jun 23 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
static prevents duplicate definition errors at link time.
The static keyword is a compile time directive. It generates code so that the variable is accessible only in the current implementation file. If this definition is in a header you will get a different variable in each implementation file that includes this header.

You will also get a different string.

The static keyword is how C developers resolve name re-definitions. The re-definition does not apply because there are separate static variables.

The linker is not involved.

The header file should never have code that generates instructions or allocates memory.
Jun 24 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: muser | last post by:
Karl this is albeit your program, in the program that you wrote for me I learned quite alot, i didn't realise I could return NULL for instance or even use functions without first declaring them,...
2
by: trying_to_learn | last post by:
im in the primary stages of learning C++. The book im learning from says Dont use using namespace.. directive in header file However im trying to make the following header file.I need to include...
12
by: blueblueblue2005 | last post by:
Hi, here is an example I copied from Deitel C++ book. but when I compile it, always get the above compilation error, no matter how I change the include order, please help. here is the files:...
6
by: Ravi | last post by:
Hi All: Is there any reason for declaring functions as static in a header file if that header file is going to be included in several other files? The compiler throws a warning for every such...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
13
by: giovanniparodi79 | last post by:
Hello everybody is there some utility to convert a raw image in an header file? Thanks everybody Gio
9
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
11
by: Gary Wessle | last post by:
Hi is it right to have a line like #include <path/to/header.hfor a library on my system, in my header file and use some functions provided by this library in the implementation file (file.cpp)...
10
by: Scoots | last post by:
I have the following code snippit that the compiler just won't take (vc ++ 6). I want to make a map as a member variable inside a class. So I've put it into the header file, and it won't take it...
8
by: nguillot | last post by:
Hello. If I have the following classes: class B {}; typedef B tB; if A is: class A
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.