Thread: Non-static const

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Non-static const

    Can i not declare a non-static const data member?

    Code:
    class A
    {	
    	public:
    	const int x;	
    	A()
    	{
    		x = 10;            
    	}
    };
    
    int main()
    {
    	return 0;
    }
    problem is here is, compiler allows me to declare a non-static const data member, meaning i can somehow use it. But how do i initialize it?

    Constructor Initialization fails bcoz compiler prevents assignment to a const data member,
    and initialization at declaration is prevented by ISO C++ std. that says i cannot initialize data members inside a class until they are static const

    so, why even the declaration of non-static const data member has been allowed by the compiler, when there is no way i can initialize it ?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    I'm not sure but try this is the implementation file:

    const int A::x = 10;

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    tried this :-

    Code:
    class A
    {	
    	public:
    	const int x;	
    };
    
    const int A::x=10;
    
    int main()
    {
    	return 0;
    }
    i get this error - ‘const int A::x’ is not a static member of ‘class A’

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    class A
    {	
    	public:
    	const int x;	
                    A():x(10)
    	{
    	}
    };
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Quote Originally Posted by vart View Post
    Code:
    class A
    {	
    	public:
    	const int x;	
                    A():x(10)
    	{
    	}
    };
    This is the answer you're looking for.

    This is why.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Thumbs up

    wow!

    initializer list is the only way i could initialize non-static const members

    thanx!

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It is how you should initialize almost all of your nonstatic member variables.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 AM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  3. Memory leak - need help finding
    By ChadJohnson in forum C++ Programming
    Replies: 8
    Last Post: 04-06-2005, 07:26 PM
  4. Problem with Template Function and overloaded equality operator
    By silk.odyssey in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2004, 04:30 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM