Thread: creating constant numbers

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    86

    creating constant numbers

    when i have a class , where i need objects of, and i want them all to have a CONSTANT id number, that is to be set in a loop in main? is this possible in some way?


    Code:
    for (int x = 0; x<100; x++)
    {
      class.setid (x);
    }
    ? like i tested it, it did not work, wich is logical cause u cant modify constant objects. but is their some way to make these unique constant id numbers in a loop? in main? or do i realy need to write 100 difrent classes with a difrent const id number?

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    You could use a static integer member which increments by 1 each time a new object is created
    Code:
    #include <iostream>
    
    class MyClass
    {
        static int id_numbers;
        const int object_id;
    public:
        MyClass() : object_id(id_numbers) { ++id_numbers; }
    
        int id() const;
    };
    
    int MyClass::id_numbers = 0;
    
    int MyClass::id() const
    {
        return object_id;
    }
    
    int main()
    {
        MyClass obj1;
        MyClass obj2;
        MyClass obj3;
    
        std::cout << "Object 1 - ID: " << obj1.id() << std::endl;
        std::cout << "Object 2 - ID: " << obj2.id() << std::endl;
        std::cout << "Object 3 - ID: " << obj3.id() << std::endl;
    }
    Before the program reaches main(), id_numbers is created, and initialised to zero.

    Every time a new object is created, the constructor automatically assigns the const object_id member with the value of id_numbers and then increments id_numbers ready for the next constructor call.
    Last edited by Bench82; 12-28-2006 at 06:03 PM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You have to initialize a constant data member with a member initializer:

    Code:
    class C {
      const int id;
    public:
      C(int id) : id(id) {}
    };

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    [QUOTE=Bench82]You could use a static integer member which increments by 1 each time a new object is created [CODE]#include <iostream>

    man, your a genius, why didnt i think of that! thnx man

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parse Error Before String Constant?
    By xombie in forum C Programming
    Replies: 4
    Last Post: 10-08-2004, 01:17 AM
  2. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM