CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2011
    Posts
    4

    Question 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 ?

  2. #2
    Join Date
    Jun 2011
    Posts
    4

    Re: static const & literal

    Is there any solution to this without have to write the string twice in the Code?

  3. #3
    Join Date
    Jul 2002
    Posts
    2,543

    Re: static const & literal

    What about good old #define directive?

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: static const & literal

    First, I wonder how you got this to compile in the first place (aside from the missing semicolon):

    Quote Originally Posted by Gener4tor View Post
    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.

  5. #5
    Join Date
    Jul 2002
    Posts
    2,543

    Re: static const & literal

    Quote Originally Posted by Eri523 View Post
    Alex F was quicker yet again...
    Maybe because my answer was a bit shorter

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: static const & literal

    Quote Originally Posted by Alex F View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured