Enumerated Constants

This is a discussion on Enumerated Constants within the C++ Programming forums, part of the General Programming Boards category; Could someone please explain Enumerated Constants to me? I dont understand them...thanks....

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    12

    Enumerated Constants

    Could someone please explain Enumerated Constants to me?
    I dont understand them...thanks.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Another use for enumerations is within class scope.
    for instance
    Code:
    class Blueprint
    {
         private:
               const int Len=30;  // error declaring a constant with class scope is an error because no object yet exists,  until you create an object there is no place for the compiler to store a value.
    
    instead:
    
    class Blueprint
    {
         private:
               enum {Len=30};//class specific constant does not create a class data member
    Got this from c++ primer plus 3rd edition

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    However that's abuse of enums. The correct way to do a class-scoped constant is to make it static:
    Code:
    class HasConstant
    {
    public:
      static const int THE_CONSTANT;
    };
    
    // Implementation file:
    const int HasConstant::THE_CONSTANT = 234;
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Ahh things you learn from people and not from books thanks. (Although books are written by people lol)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with variables and constants.
    By dave1339 in forum C Programming
    Replies: 2
    Last Post: 12-21-2008, 08:18 PM
  2. Replies: 7
    Last Post: 11-12-2008, 10:00 AM
  3. define constants
    By shuo in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2007, 03:17 PM
  4. Replies: 1
    Last Post: 02-06-2003, 02:33 PM
  5. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 07:29 PM

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