I am looking for a graceful way to declare a string const that is to be
visible across many files.
If I do this:
//----hdr.h
const char * sFoo = "foo";
//file.cpp
#include <hdr.h>
strcpy(string, sFoo);
//anotherfile.cpp
#include <hdr.h>
strcpy(string, sFoo);
The linker complains that sFoo is multiply defined.
I don't want to use a #define as it breaks type safety. I don't want to have
multiple copies of '
const char * sFoo = "foo";' littering my code.
What is the most compact and maintainable way to do this?
RDeW 12 16222
Riley DeWiley wrote: I am looking for a graceful way to declare a string const that is to be visible across many files.
If I do this:
[snip example] The linker complains that sFoo is multiply defined.
I don't want to use a #define as it breaks type safety. I don't want to have multiple copies of ' const char * sFoo = "foo";' littering my code.
What is the most compact and maintainable way to do this?
RDeW
Header file:
extern const char sFoo[];
In (one) source file (exactly which doesn't matter):
const char sFoo[] = "foo";
--
λz.λi.i(i((λ n.λm.λz.λi.n z(λq.mqi))((λ n.λz.λi.n(nzi )i)(λz.λi.i(( (λn.λz.λi.n
(nzi)i)(λz.λi .i(iz)))zi)))(( λn.λz.λi.n(n zi)i)(λz.λi.i (iz)))zi))
Riley DeWiley wrote: I am looking for a graceful way to declare a string const that is to be visible across many files.
If I do this:
//----hdr.h
const char * sFoo = "foo";
//file.cpp #include <hdr.h>
strcpy(string, sFoo);
//anotherfile.cpp #include <hdr.h>
strcpy(string, sFoo);
The linker complains that sFoo is multiply defined.
I don't want to use a #define as it breaks type safety. I don't want to have multiple copies of ' const char * sFoo = "foo";' littering my code.
What is the most compact and maintainable way to do this?
If you declare it in the header
const char * const sFoo = "foo";
which creates multiple copies. To avoid that you could do
extern const char sFoo[];
in the header and in _one_of_the_C++ _source_files_ do
extern const char sFoo[] = "foo";
The linker will be happy and you will have the only definition of the
string in the program.
V
Riley DeWiley wrote: I don't want to use a #define as it breaks type safety.
No, it doesn't.
#define sFoo "foo"
Every place you use the identifier sFoo you'll get a string literal. Its
type, however, is array of const char rather than pointer to const char,
which is what your code uses.
I don't want to have multiple copies of ' const char * sFoo = "foo";' littering my code.
What is the most compact and maintainable way to do this?
In the header:
const char *sFoo;
In one implementation file:
const char *sFoo = "foo";
The suggestion the other messages make, to use const char sFoo[], also
works, but just like the macro, it makes sFoo a different type from what
you asked for.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
Pete Becker wrote: Riley DeWiley wrote: I don't want to use a #define as it breaks type safety. No, it doesn't.
#define sFoo "foo"
Every place you use the identifier sFoo you'll get a string literal. Its type, however, is array of const char rather than pointer to const char, which is what your code uses.
I don't want to have multiple copies of ' const char * sFoo = "foo";' littering my code.
What is the most compact and maintainable way to do this?
In the header: const char *sFoo;
In one implementation file: const char *sFoo = "foo";
This is wrong. The first will create a pointer, initialized to NULL, in each
file it's included in. The second will create yet another pointer,
initialized to pointing to the constant string needed. The suggestion the other messages make, to use const char sFoo[], also works, but just like the macro, it makes sFoo a different type from what you asked for.
The array will automatically cast into const char * when needed, so it
doesn't matter.
--
λz.λi.i(i((λ n.λm.λz.λi.n z(λq.mqi))((λ n.λz.λi.n(nzi )i)(λz.λi.i(( (λn.λz.λi.n
(nzi)i)(λz.λi .i(iz)))zi)))(( λn.λz.λi.n(n zi)i)(λz.λi.i (iz)))zi))
Victor Bazarov wrote: Riley DeWiley wrote: I am looking for a graceful way to declare a string const that is to be visible across many files.
If I do this:
//----hdr.h
const char * sFoo = "foo";
//file.cpp #include <hdr.h>
strcpy(string, sFoo);
//anotherfile.cpp #include <hdr.h>
strcpy(string, sFoo);
The linker complains that sFoo is multiply defined.
I don't want to use a #define as it breaks type safety. I don't want to have multiple copies of ' const char * sFoo = "foo";' littering my code.
What is the most compact and maintainable way to do this?
If you declare it in the header
const char * const sFoo = "foo";
which creates multiple copies. To avoid that you could do
extern const char sFoo[];
in the header and in _one_of_the_C++ _source_files_ do
extern const char sFoo[] = "foo";
The linker will be happy and you will have the only definition of the string in the program.
V
Const declarations by default have internal linkage, so the declaration
can remain in the header file once it's changed from a pointer to an
array. No source files need to be changed.
In other words change this declaration in the header file:
const char * const sFoo = "foo";
to this:
const char const sFoo[] = "foo";
And the multiple definition error will be fixed - no matter how many
source files actually include sFoo's declaration.
Greg
Bryan Donlan wrote: Pete Becker wrote:
I don't want to have multiple copies of ' const char * sFoo = "foo";' littering my code.
What is the most compact and maintainable way to do this?
In the header: const char *sFoo;
In one implementation file: const char *sFoo = "foo";
This is wrong. The first will create a pointer, initialized to NULL, in each file it's included in.
You're right: it needs an extern in front.
The suggestion the other messages make, to use const char sFoo[], also works, but just like the macro, it makes sFoo a different type from what you asked for.
The array will automatically cast into const char * when needed, so it doesn't matter.
First, a cast is something you write in your code to tell the compiler
that you want a conversion. Second, there are two contexts in which the
conversion from array into pointer to its first element is not done. The
two types are not the same.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
Pete Becker wrote: Second, there are two contexts in which the conversion from array into pointer to its first element is not done. The two types are not the same.
Actually, that's true in C. In C++ there are more. And, in both
languages, this is not a conversion, but a decay: the name of an array
decays into a pointer to its first element in most contexts. And, of
course, it is still true that the two types are not the same. Try this:
a.c
---
char text[] = "abcd";
void f()
{
puts(text);
}
b.c
---
extern char *text;
void f();
int main()
{
*text = 'e';
f();
return 0;
}
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
Riley DeWiley <ri***********@ gmail.com> wrote: I am looking for a graceful way to declare a string const that is to be visible across many files.
If I do this:
//----hdr.h
const char * sFoo = "foo";
//file.cpp #include <hdr.h>
strcpy(string, sFoo);
//anotherfile.cpp #include <hdr.h>
strcpy(string, sFoo);
The linker complains that sFoo is multiply defined.
I don't want to use a #define as it breaks type safety. I don't want to have multiple copies of ' const char * sFoo = "foo";' littering my code.
What is the most compact and maintainable way to do this?
Would an #include guard work?
//-----hdr.h
#ifndef HDR_H
#define HDR_H
const char* sFoo = "foo";
#endif
--
Marcus Kwok
Riley DeWiley <ri***********@ gmail.com> wrote: I am looking for a graceful way to declare a string const that is to be visible across many files.
[...]
What is the most compact and maintainable way to do this?
Probably not the most compact way, but I suppose it would fit into the
maintainable and correct categories.
//--- MagicString.h
#ifndef MAGIC_STRING_H_ _
#define MAGIC_STRING_H_ _
extern const char* const gMagicString;
#endif
//--- MagicString.cpp
#include "MagicStrin g.h"
const char* const gMagicString = "I am a magic string!";
//--- test.cpp
#include "MagicStrin g.h"
#include <iostream>
int main()
{
std::cout << gMagicString << '\n';
}
Regards,
--
Ney André de Mello Zunino This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: selder21 |
last post by:
Hello,
I have a class with constructor taking a const string&. Now i want to
call this constructor with a string literal. Because this is of type
char* there are overload resolution conflicts.
If i make another constructor with parameter const char*, how can i
call the constructor with the const string& ?
I tried
|
by: sam |
last post by:
Hi,
Is there any way I can prevent people use some binary disambler (eg.
strings in unix) to view the const string value in a compiled C++ program?
Sam.
|
by: QQ |
last post by:
How to define a string?
Usually we can
#define MAX 30
However if I'd like to define a string const
can I?
#define S "Hello"
|
by: Erik Tamminga |
last post by:
Hi,
I'm totally bluffed: how can a 'public const string name = "myname";' ever
evaluate to null?
I have the following class:
public class MIB2
{
public const string org = "1.3";
|
by: ESPN Lover |
last post by:
I'm fairly new to the whole .NET and OOP programming so bear with me.
In the past if I wanted to have constants defined that I could use in my
code, I'd include them either the file or as an include file. But with .NET
and OOP, there has to be a different way than just including them in each of
the classes I want to utilize them in.
Here's an example of what's in the definition of each of the classes I need
to use it in.
| |
by: msaladin |
last post by:
Hi all,
I spent today with finding an error, I found it, though I don't no why.
I have a class with static constants, like this:
class EXPORT_API BusConstants {
public:
static const std::string PIPE_DIRECTORY;
};
|
by: Jae |
last post by:
Real(const string &fileName)
{
FILE * myInputFile = fopen(fileName, "rt");
.....
fclose(myInputFile);
|
by: martin-g |
last post by:
Hi.
Almost every application have to write out some messages to the user.
The question is how to store them. For example, while programming for
Windows in C++ we could store these messages as string resource and
load them using LoadString API function. I'm quite new to C#, and the
best thing I've managed is creating private constant members of a
class. E. g.:
public class ExpandManager
|
by: Ook |
last post by:
I have a function getStuff, and two choices of implementation:
const string *getStuff()
{
return &_stuff;
}
or
const string getStuff()
|
by: wizofaus |
last post by:
Given the following code:
public class Test
{
static unsafe void StringManip(string data)
{
fixed (char* ps = data)
ps = '$';
}
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |