473,321 Members | 1,748 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,321 software developers and data experts.

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::PIPE_DIRECTORY

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

Category& UBusHelper::logger =
Category::getInstance(BusConstants::PIPE_DIRECTORY );

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::getInstance. 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::PIPE_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 11671
ms******@gmx.net 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::PIPE_DIRECTORY

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

Category& UBusHelper::logger =
Category::getInstance(BusConstants::PIPE_DIRECTORY );

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::getInstance. 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.net 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::PIPE_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::logger =
Category::getInstance(BusConstants::PIPE_DIRECTORY );

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::getInstance. 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::PIPE_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
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
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
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...
3
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
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...
6
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
by: wizofaus | last post by:
Given the following code: public class Test { static unsafe void StringManip(string data) { fixed (char* ps = data) ps = '$'; }
7
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...
5
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,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.