A month or so ago I read a discussion about putting const ints in
header files, and how one shouldn't put things in header files that
allocate memory, etc. because they will generate multiple definition
errors if the header file is #include'd in more than one code file.
The answer was that constants have internal linkage unless declared
extern, so it's OK.
So, you can put something like...
const int abc = 123;
.... in a header file and be fine (in C++, not in C).
I have run into a related problem.
In one of my header files I have:
const int maxLen = 128;
const char* theMsg = "Hello, world";
This header file is #include'd in about eleventy-gazillion places
throughout the system.
When I compile, the compilerand linker is perfectly happy with the
const int, but generates a slew of "multiple definition" errors at
link time for the const char*.
What's the difference between const int and const char* that would
make one work and the other not? 4 5857
C. J. Clegg wrote: A month or so ago I read a discussion about putting const ints in header files, and how one shouldn't put things in header files that allocate memory, etc. because they will generate multiple definition errors if the header file is #include'd in more than one code file.
The answer was that constants have internal linkage unless declared extern, so it's OK.
Do they?
So, you can put something like...
const int abc = 123;
.... in a header file and be fine (in C++, not in C).
What happens if you take its address in more than one source file?
I have run into a related problem.
In one of my header files I have:
const int maxLen = 128;
const char* theMsg = "Hello, world";
This header file is #include'd in about eleventy-gazillion places throughout the system.
When I compile, the compilerand linker is perfectly happy with the const int, but generates a slew of "multiple definition" errors at link time for the const char*.
What's the difference between const int and const char* that would make one work and the other not?
My guess is that the const int does not require a memory location,
unless you take its address.
The const char* is a string literal that will be duplicated.
I always use extern for constant declarations, with the definition in a
appropriate source file.
--
Ian Collins.
In article <29********************************@4ax.com>,
C. J. Clegg <re****************@nospam.no> wrote: A month or so ago I read a discussion about putting const ints in header files, and how one shouldn't put things in header files that allocate memory, etc. because they will generate multiple definition errors if the header file is #include'd in more than one code file.
The answer was that constants have internal linkage unless declared extern, so it's OK.
So, you can put something like...
const int abc = 123;
... in a header file and be fine (in C++, not in C).
I have run into a related problem.
In one of my header files I have:
const int maxLen = 128;
const char* theMsg = "Hello, world";
Try this:
const char* const theMsg = "Hello, world";
See if that gets rid of the errors.
This header file is #include'd in about eleventy-gazillion places throughout the system.
When I compile, the compilerand linker is perfectly happy with the const int, but generates a slew of "multiple definition" errors at link time for the const char*.
What's the difference between const int and const char* that would make one work and the other not?
The difference is that "const int" is const, whereas "const char*" is
mutable (even if that to which it points is not.)
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Daniel T. wrote: I have run into a related problem.
In one of my header files I have:
const int maxLen = 128;
const char* theMsg = "Hello, world";
Try this:
const char* const theMsg = "Hello, world";
See if that gets rid of the errors.
That should. In the OP's version, theMsg isn't a const, it just points
to const data, and therefore theMsg has external linkage.
In Daniel's version, in addition to pointing at const data, the pointer
itself is a const, and therefore should have internal linkage.
On Mon, 27 Mar 2006 23:46:32 GMT, "Daniel T." <po********@verizon.net>
wrote: Try this:
const char* const theMsg = "Hello, world";
See if that gets rid of the errors.
Yup, that did it, thanks! :-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: CoolPint |
last post by:
Can anyone clearly explain the difference between constant reference to
pointers and reference to constant pointers?
What is const int * & ?
Is it a constant reference to a pointer to an...
|
by: Ajax Chelsea |
last post by:
can not the "static const int" be replaced by "static enum" anywhere?
is it necessary that define special initialization syntax for "static const int"?
|
by: nick |
last post by:
1.int const x=5;
const int x=5;
2.const char *p=new char;
char const *p=new char;
the above pairs are equivalent?
thanks!
|
by: recover |
last post by:
#include <string>
#include <iostream>
using namespace std;
class TConst
{
private:
string con;
string uncon;
public:
|
by: qingning |
last post by:
Hi,
I've encountered code declaring functions with "const int" as parameter
type, but use "int" when defining the function. This worked fine with
g++, but failed at linking time with Sun CC. ...
|
by: Lionel B |
last post by:
I have a function which takes a functor argument. I which to call it for a
functor which is actually a class member; this works fine, using
the mem_fun_ref and bind1st functions (see listing 1...
|
by: Jess |
last post by:
Hello,
If I have a constant array, i.e. it's elements aren't changed, should
I declare it as:
const int a = {1,2,3};
or
int const a = {1,2,3}
|
by: Luna Moon |
last post by:
Hi all,
I just couldn't get myself clear about the usage of "const" in front
of and/or behind variables, pointers, classes, objects and
functions...
It's too confusing... any good clear...
|
by: Bill Davy |
last post by:
I want to be able to write (const char*)v where v is an item of type
Class::ToolTypeT where ToolTypeT is an enumeration and I've tried
everything that looks sensible. There's an ugly solution, but...
|
by: Rina0 |
last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |