M
msaladin
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:IPE_DIRECTORY
Then, in some other class, I want to initialize a static logger with
this string:
Category& UBusHelper::logger =
Category::getInstance(BusConstants:IPE_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: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
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:IPE_DIRECTORY
Then, in some other class, I want to initialize a static logger with
this string:
Category& UBusHelper::logger =
Category::getInstance(BusConstants:IPE_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: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