Thread: instances w/in an array

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    12

    instances w/in an array

    I'm trying to create an instance of class Chicken using the element in the array handles[7].

    Code:
    	char *temp;
    char *handles[7]="Chick","Duck","Green", "Hawk","Jab","peacock","Red"}; 
    	
    
                    temp=handles[0];
    	Chicken temp;
    Is there a way to create an object with these names in the array?

    Thanks for any help provided.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You won't be able to give them that name in code. At least not dynamically. However, no user of your code will ever really see the internal names. Instead, perhaps you want to give your class a string member ("name" perhaps), and then give your user access to that.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    I don't know why you want to do this..but if you know the names of the classes already, why are you trying to create your class names this way? We might be able to find an alternative for you if we know what your desire in code is.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    12
    Well in this program, the user does not have access to the internal names. I'm just wondering if I can store instances/object names in an array. I must then generate a random integer within the range of array indices. Then I would use that integer to index into the array and create an object to access each classes memeber functions.

    Thanks for the replies.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    The best you can get using C++ is a map with the name as the key and the object as your value. The biggest problem I see is creating a heterogeneous data structure. If all of your classes are derived from one base class then the solution is simple.
    Code:
    #include <map>
    #include <string>
    
    class Avian;
    class Chicken: public Avian;
    
    std::string handles[] = {
      "Chick", "Duck", "Green", "Hawk", "Jab", "peacock", "Red"
    };
    std::map<std::string, Avian*> birdie;
    
    birdie[handles[0]] = new Chicken;
    
    ...
    
    birdie["Chick"]->chickmf();
    If you do not have a common base class for all of the classes then the solution is more difficult, or at least more error prone.
    Code:
    std::map<std::string, void*> thingie;
    
    thingie[handles[0]] = new Chicken;
    
    ...
    
    ((Chicken*)thingie["Chick"])->chickmf();
    In my experience, something like this is an indication of awkward program design. Perhaps you should look at your overall structure and verify that what you want is actually the best solution.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    12
    This is a simple breakdown of my procedures:

    Use a randomly generated integer to index into the array of size 7.
    Display the result of sending that array element the getKind(), getMovement(), and getCall() messages
    Code:
    int main()
    {
    	char *temp;
    	char *handles[7]={"Chick","Duck","Green", "Hawk","Jab","peacock","Red"}; 
    	
                    int i = rand()%8;
    	temp=handles[i];
    	Chicken temp;
                    temp.getKind();
                    temp.getMovement();
                    temp.getCall();
    
    return 0;
    }
    The getKind(), getCall(), getMovement() are the member functions for each bird class.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I think I see what you are saying.
    You don't want to name each object necessarily. Instead, you want to have all of your objects accessible through an array. Is this correct?

    If so, then you can simply create an array of your object type:
    Code:
    Assuming a deaful ctor:
    Chicken array[7]; // Creates an array of seven Chicken objects.
    
    ... or ...
    Chicken* array = new Chicken[7];
    // Do some stuff...
    delete[] array;
    
    ... or, the preferable ...
    std::vector<Chicken> array;
    for(int i = 0; i != 7; ++i)
       array.push_back(Chicken()); // Create unnamed instances of the Chicken object, placing them immediately into the vector.
    You can find more documentation on the STL Vector here : www.sgi.com/tech/stl

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Right... I didn't see the above two posts before I replied.

    Go with the std::vector. You can do exactly what you want. Play around with the class a little, and let us know if you need some help with it.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    how about create an array of chicken instances.
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM