Thread: Help with const

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41

    Help with const

    I am working on a program in which I have the struct
    Code:
    struct molecule {
    	double x;
    	double y;
    	double z;
    	double const radius = .05;
    };
    When I compile with g++ without the pedantic flag everything works fine; however, if I use the pedantic flag I get the following error:
    Code:
    poundcake.h:34: error: ISO C++ forbids initialization of member ‘radius’
    poundcake.h:34: error: making ‘radius’ static
    poundcake.h:34: error: ISO C++ forbids initialization of member constant ‘radius’ of non-integral type ‘const double’
    I have read that you can only use const with types int and enum. Why is this? I don't understand why it would be a bad idea to make a double const.

    Also, is there any difference between writing
    Code:
    const int x
    versus
    Code:
    int const x
    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by waterborne
    I have read that you can only use const with types int and enum. Why is this? I don't understand why it would be a bad idea to make a double const.
    Your information is either incorrect or misinterpreted. You are allowed to initialise static const integer members directly with their declaration in the class definition. For static const non-integer members, you should perform the initialisation elsewhere, e.g., in a source file. For non-static const members, of whatever type, you should perform the initialisation in a constructor (or by using an aggregate initialiser, like how you might initialise an array).

    Quote Originally Posted by waterborne
    Also, is there any difference between writing
    There is no real difference.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM