comparing with namespace variable
i am using namespace in header file as under
namespace MyNameSpace
{
extern string data;
};
开发者_如何学编程And in implementation file as under.
namespace MyNameSpace
{
string data = "Data";
};
Then in some point of my code i am comparing as under.
string mData = "Data";
if(mData == MyNameSpace::data)
{
//do something
}
But, when i compare it is not going in the segment. What could be the reason for this.
Regards, Lenin
I cannot definately be sure that this is causing your particular problem because I do not know the rest of the code. However, it should definately apply anyway:
From the Google C++ Style Guide:
Static or global variables of class type are forbidden: they cause hard-to-find bugs due to indeterminate order of construction and destruction. Objects with static storage duration, including global variables, static variables, static class member variables, and function static variables, must be Plain Old Data (POD): only ints, chars, floats, or pointers, or arrays/structs of POD.
The order in which class constructors and initializers for static variables are called is only partially specified in C++ and can even change from build to build, which can cause bugs that are difficult to find. Therefore in addition to banning globals of class type, we do not allow static POD variables to be initialized with the result of a function, unless that function (such as getenv(), or getpid()) does not itself depend on any other globals.
Likewise, the order in which destructors are called is defined to be the reverse of the order in which the constructors were called. Since constructor order is indeterminate, so is destructor order. For example, at program-end time a static variable might have been destroyed, but code still running -- perhaps in another thread -- tries to access it and fails. Or the destructor for a static 'string' variable might be run prior to the destructor for another variable that contains a reference to that string.
As a result we only allow static variables to contain POD data. This rule completely disallows vector (use C arrays instead), or string (use const char []).
If you need a static or global variable of a class type, consider initializing a pointer (which will never be freed), from either your main() function or from pthread_once(). Note that this must be a raw pointer, not a "smart" pointer, since the smart pointer's destructor will have the order-of-destructor issue that we are trying to avoid.
Long story short: Constannts of type "string" should never be used. You may want to use something like
const char data[] = "Data";
You have one = That is the assign operator.
You need mData == MyNameSpace::data
With mData = MyNameSpace::data the value of the expression, is different than zero (it's the address where "Data" is stored) so it is true (everything different than zero is treated as true).
Edit: If you compile with gcc, some flags like -Wall might help you find this kind of bugs / errors.
Could you give us a small, compilable example of the problem. The code that you posted is fine; most likely, one of the two variables doesn't contain what you think it does.
精彩评论