-
June 21st, 2011, 09:10 AM
#1
static const & literal
Hi...I have a question concerning const/literal-functions and members
c++:
struct StyleConstants
{
static const CString TestConst="UniqueStringWhichShouldBeOnlyWrittenOnceInTheCode";
}
cli:
public ref class StyleConstNet
{
literal String^ TestConst=StyleConstants::TestConst;
}
but the code retrifes an Error:
error C3887: 'StyleConstNet::TestConst: the initializer for a literal data member must be a constant expression
The main problem is: I have to use StyleConstNet.TestConst like this:
case StyleConstNet.TestConst:
inside an switch-statement.
Is there any solution to this without ?
-
June 21st, 2011, 09:12 AM
#2
Re: static const & literal
Is there any solution to this without have to write the string twice in the Code?
-
June 21st, 2011, 10:34 AM
#3
Re: static const & literal
What about good old #define directive?
-
June 21st, 2011, 10:42 AM
#4
Re: static const & literal
First, I wonder how you got this to compile in the first place (aside from the missing semicolon):
Originally Posted by Gener4tor
c++:
struct StyleConstants
{
static const CString TestConst="UniqueStringWhichShouldBeOnlyWrittenOnceInTheCode";
}
When I try this (with std::string in place of the CString though, because I don't have MFC here), I get an error C2864. It only works for me with TestConst at file scope (however, you may put it into a namespace).
error C3887: 'StyleConstNet::TestConst: the initializer for a literal data member must be a constant expression
Unfortunately this is the plain truth. The initializer for a literal member must be a compile-time constant. The CString, however, is an object that needs to be constructed at runtime, so it doesn't work that way. (And I'm not even sure you actually can simply assign a CString to a .NET System::String ^ at all, even away from the context of initialization.)
If you really don't want to write the constant twice in your code (which I can understand because it complicates maintainance), I think the adequate solution here is a really trivial one, #define the string literal as s macro:
Code:
#define TESTCONST "UniqueStringWhichShouldBeOnlyWrittenOnceInTheCode"
// c++:
struct StyleConstants
{
static const CString TestConst=TESTCONST;
}
// cli:
public ref class StyleConstNet
{
literal String^ TestConst=TESTCONST;
}
Please use code tags when posting code.
Ah, and... welcome to CodeGuru!
EDIT: Alex F was quicker yet again...
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
June 21st, 2011, 06:29 PM
#5
Re: static const & literal
Originally Posted by Eri523
Alex F was quicker yet again...
Maybe because my answer was a bit shorter
-
June 21st, 2011, 06:47 PM
#6
Re: static const & literal
Originally Posted by Alex F
Maybe because my answer was a bit shorter
Certainly. You know I always tend to those elaborate (wordy?) replies...
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|