const in class

This is a discussion on const in class within the C++ Programming forums, part of the General Programming Boards category; hi guys i have a question about constant variable i tried to define consts in a class but compiler gives ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    const in class

    hi guys i have a question about constant variable i tried to define consts in a class but compiler gives errors.do you guys have any suggestion?can i use #define instead of const?what wrong with this code?



    Code:
    //this class calculates circle perimeter of a circle   ,c=2*pi*r
    
    class circle 
    {
    	const float pi=3.14;
    	const int r=2;
    	const int m=4;
    public:
    	
    	void calc()
    
    	{
    
    //arguments
    
    
    }
    
    
    };

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    You can't define const members that way. In your case, i suggest using 'static const'.
    Devoted my life to programming...

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    For your purpose, static const will be fine. Otherwise you have to initialize them using the constructor (using initializer lists).
    That will thankfully be fixed in the next version of the standard.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Members cannot be initialized this way. Const members/references must be all initialized in all constructors. These "constants" should not be non-static members.
    Code:
    static const float pi = 3.14;
    class circle 
    {
        int r_; // float?
        int m_; // float?
    public:
        circle(int r, int m)
            : r_(r), m_(m)
        {
        }
    };
    I never put signature, but I decided to make an exception.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    19
    good !

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Const class members
    By Phenom in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2011, 11:39 AM
  2. Replies: 3
    Last Post: 04-10-2009, 02:20 AM
  3. Replies: 1
    Last Post: 04-03-2009, 08:52 AM
  4. Defining const in a class
    By g4j31a5 in forum C++ Programming
    Replies: 5
    Last Post: 11-20-2006, 10:27 AM
  5. Class functions can't use const =/
    By neandrake in forum C++ Programming
    Replies: 10
    Last Post: 12-02-2004, 12:48 AM

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