-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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
-
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.
-
how about create an array of chicken instances.