In this code I have two forward declarations, a bool
array and a QString
array in namesace NLog
.
bool
works. QString
produces a segmentation fault.
Header:
class Log : public QObject
{
explicit Log();
public:
enum Facility
{
third_party_fac,
test_fac,
__facility_last_element
};
enum Severity
{
debug_sev,
warning_sev,
critical_sev,
fatal_sev,
__severity_last_element
};
};
namespace NLog
{
extern bool logging_enabled[Log::__facility_last_element][Log::__severity_last_element];
extern QString severity_name[Log::__severity_last_element];
};
class LogStaticInitiallizer
{
public:
LogStaticInitiallizer();
};
static LogStaticInitiallizer initiallizer=LogStaticInitiallizer();
Source:
QString NLog::severity_name[Log::__severity_last_element];
bool NLog::logging_enabled[Log::__facility_last_element][Log::__severity_last_element];
using NLog::logging_enabled;
using NLog::severity_name;
LogStaticInitiallizer::LogStaticInitiallizer()
{
qDebug()<<"0";
qDebug()<<logging_enabled[0][0];
qDebug()<<severity_name[0];
qDebug()<<"1";
}
it produces
0
false
and crashes at QString trace
As Piotr S. said, the problem is that
LogStaticInitiallizer
constructor is called before its initialization in source file.Also, there is an unrelated to main problem with reserved identifiers starting with
__
. See What are the rules about using an underscore in a C++ identifier?