473,804 Members | 3,822 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const string in static class

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;
};

Somewhere else, I declare the String:

const std::string BusConstants::P IPE_DIRECTORY

Then, in some other class, I want to initialize a static logger with
this string:

Category& UBusHelper::log ger =
Category::getIn stance(BusConst ants::PIPE_DIRE CTORY);

What is very weird is that I get segmentation faults on Linux and
Solaris, but everything seems to work fine on Windows. My workaround is
to define the string as "const char*", and then it works, because the
compiler automatically remarks this and seems to create a new string
object when I call Category::getIn stance. Here is how it works:

class EXPORT_API BusConstants {
public:
static const char* PIPE_DIRECTORY;
};

Somewhere else, I declare the char pointer:

const char* BusConstants::P IPE_DIRECTORY

Does somebody have any idea? My only guess is that static const strings
are not created until some constructor or anything is called. Because
in the class with my constants (BusConstants) I don't have any
constructor, this is why it is only initialized when some other method
of BusConstants is called.I used to program quite a bit in Java, there
this is different? I very appreciate if anybody could reassure my
assumption. Sorry, if this is a bit a naive question.

By the way: The segmentation fault did not happen every time. It only
happened when I had some weird order in how I linked my object files
(one object file per C++ class).

Thanks a lot.
Michael

Mar 28 '06 #1
2 11711
ms******@gmx.ne t wrote:
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;
};

Somewhere else, I declare the String:

const std::string BusConstants::P IPE_DIRECTORY

Then, in some other class, I want to initialize a static logger with
this string:

Category& UBusHelper::log ger =
Category::getIn stance(BusConst ants::PIPE_DIRE CTORY);

What is very weird is that I get segmentation faults on Linux and
Solaris, but everything seems to work fine on Windows. My workaround is
to define the string as "const char*", and then it works, because the
compiler automatically remarks this and seems to create a new string
object when I call Category::getIn stance. Here is how it works:


This is a FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-10.12

There are solutions there as well.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 28 '06 #2
ms******@gmx.ne t wrote:
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;
};
It's a good idea to reserve names with all uppercase for macros only.
Somewhere else, I declare the String:
You mean you define it. The above was the declaration.
const std::string BusConstants::P IPE_DIRECTORY
Without an initializer, a constant is pretty useless.
Then, in some other class, I want to initialize a static logger with
this string:

Category& UBusHelper::log ger =
Category::getIn stance(BusConst ants::PIPE_DIRE CTORY);

What is very weird is that I get segmentation faults on Linux and
Solaris, but everything seems to work fine on Windows.
My workaround is to define the string as "const char*", and then it
works, because the compiler automatically remarks this and seems to create
a new string object when I call Category::getIn stance. Here is how it
works:

class EXPORT_API BusConstants {
public:
static const char* PIPE_DIRECTORY;
};

Somewhere else, I declare the char pointer:

const char* BusConstants::P IPE_DIRECTORY

Does somebody have any idea? My only guess is that static const strings
are not created until some constructor or anything is called.
Well, static class members are created at program startup, and part of that
creation is that the constructor is called. However, there is no specific
order in which objects defined in different translation units are created.
So it's likely that on the Linux and Solaris machines, you were lucky,
because the string was created after the logger. Otherwise, you wouldn't
have noticed the error.
Because in the class with my constants (BusConstants) I don't have any
constructor, this is why it is only initialized when some other method
of BusConstants is called.
Static members don't have anything to do with the constructor. They belong
to the class, not to instances of it. They are all created at program
startup.
I used to program quite a bit in Java, there this is different? I very
appreciate if anybody could reassure my assumption. Sorry, if this is a
bit a naive question.

By the way: The segmentation fault did not happen every time. It only
happened when I had some weird order in how I linked my object files
(one object file per C++ class).


The oder of initialization might (but isn't required to) depend on the order
in which you give the object files to the linker.

Mar 28 '06 #3

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

Similar topics

15
8066
by: Wolfram Humann | last post by:
Hi, please don't be too harsh if I made stupid errors creating this simple example from my more complex case. Suppose I have a class like this: class BOOK { const string title;
5
1715
by: Minti | last post by:
How do we initialize static const data in C++, I tried class Foo { static const std::string name = "MyName"; }; I don't see any reason as to why this must not work.
4
9497
by: Steven T. Hatton | last post by:
#include <iostream> namespace ns{ const char name = "This is a Class Name";//won't compile //char name = "This is a Class Name"; // compiles template <typename T, char* Name_CA=name> struct A { A() : _data(15)
3
2117
by: usenet | last post by:
Kindly pardon my ignorance, but what is the way to declare a constant attribute of type string within a class? Thanks, Gus
2
2257
by: Adrian | last post by:
Hi, In a header file I tried const char *someval="this is a test"; which is included all over the place and I get linker errors about multiple defines. Why is this not folded when const char someval="this is a test"; is and I get no linker errors?
6
15673
by: DaTurk | last post by:
I'm converting MC++ to C++/CLI and for some reason it's not cool with static const System::String^ How am I supposed to have static constant strings then?
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 = '$'; }
7
3054
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 surely this is possible? I could define an operator<< but for various reasons, I really want to convert to a 'const char*' (to embed into a string which becomes part of a window's caption, etc).
5
3082
by: Bob Doe | last post by:
I have a const static object. What is the right syntax to get a reference to the std::vector within the std::map variable?: class MyObj { public: ... std::map<std::string, std::vector<std::string l_; };
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...
1
10320
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,...
0
10077
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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
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...
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
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
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...

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.