Life Time of a vairiabe

Hi, I was wondering is there anyway to define a variable inside a loop with global access definition. for instance how can i change use i out of the following f scope:
1
2
3
4
5
6
7
8
9
class myClass{
...
}


if(true){
   myClass i;
}
   i.print();


Thanks
Ben
i dont think that is possible...

but i may be mistaken.. sorry :(
This isn't possible as far as I know. If you need the variable outside the loop, declare it in that scope.
for instance how can i change use i out of the following f scope:


That's pretty simple:

Use a global pointer.

1
2
3
4
5
6
if (true)
{
    // alcoate memory
}
else
   // pointer = nullptr; 
@codekiddy

Yeah that was right answer, I just defined a pointer before my if condition and then I allocated memory inside id and before ending if I pointer it to the data:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	Mail* mailPtr[filenum];

	while(!fin.eof()){
		getline(fin,classtype);
		if(classtype=="regular"){
.			Regular *r = new Regular;
.
.
.
.

			mailPtr[counter] = r;
			counter++;
                }
       }
Last edited on
Topic archived. No new replies allowed.