Non-static const

This is a discussion on Non-static const within the C++ Programming forums, part of the General Programming Boards category; Can i not declare a non-static const data member? Code: class A { public: const int x; A() { x ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,457
    Code:
    class A
    {	
    	public:
    	const int x;	
                    A():x(10)
    	{
    	}
    };
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    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,317
    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, 04: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, 06:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21