-4

I am going to say this, please tell me if I'm wrong or right.

  • If I create an instance of a class inside a loop, once that loop is finished, the instance is deleted.

How can I create multiple instances of a class using a loop?

Can I do something like with variables, creating them on the heap?

Are the classes created on the stack in the first place?

Or somewhere else?

How do I properly create a 'global' instance of a class?

I'm really confused about this, thanks for any help.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • 5
    Use a std::vector<> – Sid S Apr 12 '18 at 22:06
  • 1
    Please review the book list and read one: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282 – Richard Critten Apr 12 '18 at 22:07
  • @Sid S Ah, this is the type of thing I was looking for, thanks! – thisisnotworking4meplshelp Apr 12 '18 at 22:09
  • "If I create an instance of a class inside a loop, once that loop is finished, the instance is deleted." Depends on how you create it. With `MyObject instance;`, instance goes out of scope and is destroyed. With `MyObject * pointer = new MyObject();`, `pointer` vanishes at the end of the loop, but the allocated `MyObject` remains and is leaked because no more references to it exist. Don't do the second and you are fine. – user4581301 Apr 12 '18 at 22:11
  • You can create a global instance by declaring a global variable outside any function. – Barmar Apr 12 '18 at 22:15
  • It's no different for class instances than any other type of object. – Barmar Apr 12 '18 at 22:15
  • @user4581301 But you said the first one is destroyed once you go out of the scope... And its happened to me before... what do you mean? – thisisnotworking4meplshelp Apr 12 '18 at 22:16
  • 1
    @thisisnotworking4meplshelp Variables are scoped to the loop, but objects created dynamically with `new` are permanent until you use `delete`. The pointer variable is destroyed when you leave the loop body, so you can't access the object if you haven't saved it in a global pointer. – Barmar Apr 12 '18 at 22:19
  • 1
    It seems like you need to read a good C++ book to learn all these basic concepts, we can't teach it here. – Barmar Apr 12 '18 at 22:20
  • In `C++` creating a class object is no different from creating an `int`. Everything is an object. – Galik Apr 12 '18 at 22:23
  • Based on the dearth of code you provided, I can only conclude that you are doing it wrong. – Eljay Apr 12 '18 at 22:29

1 Answers1

1

You can:

1- Make a global vector that contains pointers to your object.
2- Allocate within a loop.
3- Deallocate when not needed with another loop or individually as needed.

Example:

vector<obj*> v;  

int main(){

    int mysize = 10;

    for(int i=0; i < mysize; i++)  //to allocate on the heap
    {
        v.push_back(new obj) ;
    }

     for(int i=0; i < mysize; i++)  // to deallocate from the heap
    {
        delete v[i];
        v[i] = nullptr;
    }
}

You can alternatively push the objects directly into the vector like this:

vector<obj> v;

int main(){
    for(int i=0; i < mysize; i++) {
        v.push_back(obj()) ;
        v[i].print();
    }
}

Or in fact, if you know how many times you loop, you can just simply as Liteness suggested do it in one line:

vector<obj> v(mysize);  
Shadi
  • 1,701
  • 2
  • 14
  • 27