Help with const

This is a discussion on Help with const within the C++ Programming forums, part of the General Programming Boards category; I am working on a program in which I have the struct Code: struct molecule { double x; double y; ...

  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
    22,174
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 09:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  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, 12:58 PM

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