Thread: Initializing static const memebers in a class

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    Initializing static const memebers in a class

    I have a class that has a static const member variable in it. The variable is anouther class. I know you can't initialize it inside the class so how would I go about doing that?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Posting code helps.
    Code:
    class T
    {
       static const double pi;
    };
    
    const double T::pi = 4.0 * std::atan(1.0);
    [edit]
    Code:
    class T
    {
       int i;
    public:
       T(int i_ = 0) : i(i_) {}
    };
    
    class U
    {
       static const T t;
    };
    
    const T U::t = 0;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Yup ok that works perfectly. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Weird errors.
    By Desolation in forum C++ Programming
    Replies: 20
    Last Post: 05-09-2007, 01:10 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM