• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Coding questions about creating class instances?

JC0133

Senior member
Is it possible to instantiate a class inside of a for loop and continue to make a new instance of the class as I iterate threw the for loop?

Also can you have multiply instances with the same name but if I pass in different values in the Constructor, will all of the instances exist with the same name but with different values or will it keep over writing it self?


Can I create a string and then create an instance of animal with the variable/name of the string?


Is this possible below.


class animal {
};


string bob;


bob animal();
 
Is this homework? It smells very homeworky.

Why not try it out and see what happens? It shouldn't be too hard at all to create a test program which will answer all your questions.
 
Is this homework? It smells very homeworky.

Why not try it out and see what happens? It shouldn't be too hard at all to create a test program which will answer all your questions.

Fails the sniff test :'(

OP spends more time trying to post the questions and then coming back for the answer vs actually spending the 10 minutes (max) to test his question.
 
First off, what language is this? I know in my main language you can instantiate inside a loop, but I'm not going to assume that is true for all languages. I don't have constructors though 🙁

As far as managing the instances, you'll need to store them in an array of some sort and identify an index to reference them with. I use dictionaries for this but I run an overall small application, I get to use as much memory as I want!
 
Is it possible to instantiate a class inside of a for loop and continue to make a new instance of the class as I iterate threw the for loop?

I'm going to assume C++ for a bit. Do you know how "new" with a class works? Do you know how malloc() works with a struct? Well, they're very similar.

Can I create a string and then create an instance of animal with the variable/name of the string?


Is this possible below.


class animal {
};


string bob;


bob animal();

Syntax error at line 8 near "animal();". :sneaky: Really, I don't see what you're trying to do here.
 
Last edited:
Can I create a string and then create an instance of animal with the variable/name of the string?


Is this possible below.

class animal {
};


string bob;


bob animal();

Code:
class Animal {
  void Animal(string name) {}
}

string bob = "Bob";

Animal animal = new Animal(bob);

My C++ is rusty, but something like that.
 
Back
Top