-1
// Online C++ compiler to run C++ program online
#include <iostream>
#include <iterator>
#include <set>
int index;
int member[5] = {0,1,2,3,4};
class Animal{
    public:
    Animal(int val){
        member[val]=-1;
    }
    
    ~Animal(){
        member[index]=index;
    }
    
};

int main() {
    // Write C++ code here
    for(int i=0;i<5;i++){
        std::cout<<member[i]<<" ";
    }
    std::cout<<std::endl;
    
    for(int i=0;i<5;i++){
        index=i;
        Animal a(i);
    }
    
    for(int i=0;i<5;i++){
        std::cout<<member[i]<<" ";
    }
    return 0;
}

In following code the output is:

0 1 2 3 4

0 1 2 3 4

But I am interested in the following output:

0 1 2 3 4

-1 -1 -1 -1 -1

So every time we perform Animal a(i); its constructor gets called and member[val]=-1 but immediately after its iteration the destructor gets called which makes the value back to val. member[index]=index.

How can we delay the call to destructor in this case?

I want the member[5]={-1,-1,-1,-,1,-1} after the for loop ends

and member[5]={0,1,2,3,4} restored to original value only when main() exists.

Vasu Gupta
  • 39
  • 1
  • C++ does not work this way. When an object gets destroyed its destructor always gets called. There are no workarounds, there are no exceptions. You must figure out the correct logic so that your objects have the desired lifetime, and get destroyed in a controlled fashion. – Sam Varshavchik Sep 07 '22 at 11:45
  • 1
    Why are you even using global variables? And the variable called `member` isn't really a member of any class or object. – Some programmer dude Sep 07 '22 at 11:45
  • Hi! This is *really* a strange problem. No matter how I turn it, unless you *need* the `Animals` around after the loop, the only sensible solution is to simply not do what you do in the destructor. But there's surely a reason you do that, some architecture that you came up with that solves a problem. We don't know that problem – so we can't help you find a different way of solving it. – Marcus Müller Sep 07 '22 at 11:46
  • @SamVarshavchik There's no exception, except when there's an exception (from the constructor) :) – Some programmer dude Sep 07 '22 at 11:46
  • @Someprogrammerdude - the object never gets constructed. Hence no destructor. – Sam Varshavchik Sep 07 '22 at 11:47
  • Also, why are you including `` and ``? Those header files aren't needed or used. Overall it seems you might need to invest in [a few good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Sep 07 '22 at 11:53

1 Answers1

1

While it's an ugly hack, how about using a vector that gets destructed after the code you're interested in?

Perhaps something like:

{
    std::vector<Animal> animals;

    for(int i = 0; i < 5; ++i){
        std::cout << member[i] << ' ';
    }
    std::cout << '\n';

    for(int i = 0; i < 5; ++i){
        animals.emplace_back(i);
    }

    for(int i = 0; i < 5; ++i){
        std::cout << member[i] << ' ';
    }
    std::cout << '\n';

    // Here the vector object life-time ends
    // All Animal object will be destructed
}

As noted, this solution will use the last value of index when the Animal object are being destructed, and since it's never used by the above code it will stay at its (implicitly) initialized value of 0.

You need to store the "index" passed to the constructor in an actual member variable, and use it in the destructor:

class Animal
{
public:
    Animal(int index)
        : my_index{ index }
    {
    }

    ~Animal()
    {
        member[my_index] = my_index;
    }

private:
    int my_index;
};

After this you can remove the global index variable, as it's no longer used or needed.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621