Dynamicaly naming classes

This is a discussion on Dynamicaly naming classes within the C++ Programming forums, part of the General Programming Boards category; Hello to everybody, I'm a complete newby to c++ and in the upcomming months I wish to learn this language, ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    27

    Question Dynamicaly naming classes

    Hello to everybody,

    I'm a complete newby to c++ and in the upcomming months I wish to learn this language, (so please don't hold it against me if I use incorrect terminology; but correct me if I'm wrong...).

    During my exploration of c++, these last days, I have come up a problem for which I haven't been able to find a solution anywhere, yet I'm convinced it is possible. I hope someone can tell me how to deal with it.

    Here's the problem:

    I want to generate a lot of objects using a for loop. I want to generate then names of those objects using a variable I use in the for loop. It would look something like this:

    int i;
    for (i = 1; i < 101;i++)
    {
    class_name object_name+i;
    }

    The desired effect would be 100 objects, named
    object_name1,
    object_name2,
    object_name3,
    ...
    object_name100.

    I'm sure this is possible, but I can't work out how to acheive this.

    I would be very gratefull if someone could hand me the solution.

    Greatz,

    Mbrio

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    I don't think what you're trying to do is possible, all symbols need to be created a compile time. You may be able to knock something together using the pre-processor, but for what you want I don't think it would be necessary.

    Another problem is that you seem to be trying to create objects on the stack within a for loop. All variables created in this manner will be removed once the for loop ends.

    Why don't you just create an array of objects -

    class_name object_name[100];

    You can then use the objects index as it's number -

    object_name[0],
    object_name[1],
    object_name[2],
    ...
    object_name[99].

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    27
    After I posted this I thought of this solution myself aswell, but I didn't think it would be possible, but now you mentioned it I'll give it a go. Thanks.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    27
    Can't get it to work with an array either. Right now I have this:

    int main()
    {
    char objs[99];

    int i;
    for (i = 0; i < 100;i++)
    {
    obj objs[i];
    }
    }

    (i left the code for the class out...)

    When I compile I get these errors (for the "obj objs[i];" line) :

    expected constant expression
    cannot allocate an array of constant size 0
    'objs' : unknown size

    I guess I'm doing something wrong with the array, but don't know what...

    Or is it that you can't put objects in an array??
    please help.

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Scrap the for loop. Just declare an array of 100 obj's.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    27
    Hye, is't me again.

    I've been trying to declare an array of several obj's (2 infact...). But it doesn't seem to be possible to do that either...

    my code:

    int main()
    {
    int objs[2];

    obj objs[0]; // obj is the classname...
    obj objs[1];

    /* some more code that doesn't matter right now.. */
    }

  7. #7
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    My advice is to sit down with a 1,000+ page book and read, read, read all the way through it and then you can ask us some questions. By that time you probably won't be asking us too many since a book has alot of the answers your seeking.

    You need to learn OOP and how objects work. Classes are compiled, they have to be at the compile stage. What's dynamic is what is called an instance of an object. Once you've created a class at compile time you can make as many instances of that object as you wish at run time.

    Use an array;

    int Objs[100]; //This is an array of 100 objs of type int, 100 instances of the int class.

    You call these objects in the array the same as you would any other variable:

    Objs[0] = 5; //The first object is being set to five
    Objs[1] = 0; //The second object is being set to 0
    ...
    Objs[99]=15; //The last object is being set to 15

    You only create an instance once, same with arrays, and then you call them as variables. Our array was initialized with 100 instances but we begin counting at 0 so there is 0-99 objects: Objs[0] .........Objs[50].........Objs[99]

    *You don't call the arrayed objects by:
    objs objs[i];

    just call

    objs[i];
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    27
    I didn't think it was possible to do it in the first place, but since Sorensen mentioned it, he made me think otherwise.

    But since I'm not really going anywhere with this I guess, I'll just start reading that 1000+ book and read, read, read.

    Thanks anyway.

  9. #9
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    Magic

    When you create a user-defined type in C++, it's actually a new type. So, you make a class Objs, then do this in code:

    Objs MyObjects[100];

    so then we can do:

    MyObjects[24].DoStuff();

  10. #10
    Registered User
    Join Date
    Feb 2002
    Posts
    27
    rmullen3: you are my saviour. thanks a lot. it works. )) happy happy joyjoy. i've been working on this for hours now and you just gave me the right push in the right direction. thanks!

  11. #11
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Heh, sure.

  12. #12
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    array of pointers

    No pun to fine examples ... here is another suggestion.

    You could build an array of pointers to objects and allocated them as you need them... Since a pointer to array is only a 32bit value..
    it is cheap when compared to a complete object of umpteen plus values.... This would also allow you to free on demand...

    Code:
    outside main.....
    obj *objectArray[MAXOBJS];  //no creation here
    int   iObjectsCount;
    
    
    for(i = o; i < MACOBJS; i++)
    {
         objectArray[i] = NULL;   //set to null
    }
    
    
    ....somewhere down the row....
    void CreateNewObject( ....data.... )
    {
         objectArray[iObjectsCount++] = new obj;
    
    
         ....update with data....
         objectArray[iObjectsCount-1]->SetSomeData( ....data.... );
    
    }
    
    
    void OnShutdown()
    {
            for( int i = 0; i < iObjetcsCount; i++)
           {
                 delete objectArray[i]; //notice this is the same as not delete [] objectArray;
          }
    
           //you are deleting one obj at a time.....
    }
    zMan

  13. #13
    Registered User
    Join Date
    Feb 2002
    Posts
    27
    Thanks for the suggestion, looks good.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 11:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. Replies: 2
    Last Post: 06-18-2004, 09:08 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

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