473,804 Members | 4,128 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Where to define a const string?

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
Aug 26 '05
12 16233
Just a reminder on your include guards: words with double underscores
are reserved for the implemention.

Sep 20 '05 #11
Marcelo Pinto <mp******@gmail .com> schrieb:
Just a reminder on your include guards: words with double underscores
.... at the beginning ...
are reserved for the implemention.


Regards, Markus
Sep 25 '05 #12
Markus Becker wrote:
Marcelo Pinto <mp******@gmail .com> schrieb:
Just a reminder on your include guards: words with double underscores


... at the beginning ...


Nope: double underscores are off limits regardless of their position within
identifiers. See [17.4.3.1.2/1]:

17.4.3.1.2 Global names [lib.global.name s]

1 Certain sets of names and function signatures are always reserved
to the implementation:

? Each name that contains a double underscore (__) or begins with
an underscore followed by an uppercase letter (2.11) is reserved
to the implementation for any use.

are reserved for the implemention.

Best

Kai-Uwe Bux

Sep 25 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
9410
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
4
2802
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.
3
36385
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"
1
4118
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";
6
1938
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.
2
11711
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; };
5
8843
by: Jae | last post by:
Real(const string &fileName) { FILE * myInputFile = fopen(fileName, "rt"); ..... fclose(myInputFile);
2
2449
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
8
2051
by: Ook | last post by:
I have a function getStuff, and two choices of implementation: const string *getStuff() { return &_stuff; } or const string getStuff()
2
10145
by: wizofaus | last post by:
Given the following code: public class Test { static unsafe void StringManip(string data) { fixed (char* ps = data) ps = '$'; }
0
9579
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,...
0
10577
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
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 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...
0
9150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
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...
0
6853
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();...
0
5521
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...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4299
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 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.