Home How to create multiple global instances of a class
Reply: 0

How to create multiple global instances of a class

user8034 Published in September 19, 2018, 9:08 am

I want to create multiple global instances of a class. I tried to do this:

for (int x = 0; x <= 10; x++) {

            bunny x;
            x.create(5, 5); 


(The class name is bunny)

What I tried to do here, was to create an instance named the value that x is at the time, then use the variable x to do functions on the instance with the name the value of x.

But what was brought to my attention was that it just created an instance named num each time, and destroyed it once the loop was over. (I'm pretty new to this, so please bear with me, and the functions I've called from the class have random functions and stuff in them, this fooled me into thinking that it was indeed creating 10 different instances.)

How can I do this, so that 10 global instances are made (so they are not destroyed once the loop is over), and each instance is numbered from 1 - 10. If, like variables, the instance name can't begin with a number, then something like: a1, a2, a3, a4....

This has confused me for so long, Thanks for any help.

share|improve this question
  • like bunny bunnies[11]; and then bunnies[i].create()? – Gaurav Sehgal Apr 17 at 8:54
  • Populate map<time, bunny>. – VTT Apr 17 at 8:54
  • Have you heard about containers? They could store your elements, however I would advise you to read a book about C++ programming – M. Denninger Apr 17 at 8:54
  • I don't know what you guys mean, please explain this to me like I'm stupid. – thisisnotworking4meplshelp Apr 17 at 8:57

1 Answer 1

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.586988 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO