CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2005
    Posts
    382

    static constant - non integral type

    Code:
    // header file called .. test.h                 [1] 
    static const double dval ( 3.1233 ); 
    
    // in a source file called main.cpp        [2] 
    class  bar { 
      static const double dbar_val ; 
    public : 
      bar () {} 
    
    
    }; 
    
    
    double bar::dbar_val = 3.1233;
    [1] compiles while [2] does not because only (hope I'm saying this right) static const integral data members can be initialized in a class. I was reading a thread a few days ago where it was said - if memory serves - C++Ox (some version of GCC is already doing this) will recognize [2]. The question: Why is [1] allowed to compile while [2] doesnt? The fact that one is a class and the other is not doesnt seem like a good answer to me.

  2. #2
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: static constant - non integral type

    The problem here is simply that dbar_val is declared const but defined non-const. Change that line to:
    Code:
    const double bar::dbar_val = 3.1233;
    Note you could also do:
    Code:
    class  bar { 
      static const double dbar_val = 3.1233; 
    public : 
      bar () {} 
    }; 
    
    const double bar::dbar_val;
    I should add that static class member definitions should always appear in a cpp (or equivalent) file, never in a header unless it is a template class.
    - Alon

  3. #3
    Join Date
    Dec 2005
    Posts
    382

    Re: static constant - non integral type

    Thanks Hermit. That was a bonehead move on my part.

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