can I create objects/variables inside a function such that they will persist after the function returns?

2008-12-08 2:36 pm
I'll admit, this is for a homework assignment, though the assignment is not to answer this question. I'm not asking anyone to write my program for me, Im just asking if something is possible or not. I am to write a class that can act as a three dimensional (or less) array. the program to be utilizing my library should see no difference besides notation in its usage from a 3d array. my thoughts were if I made a class for each position with three pointers (the next x, the next y and the next z) with a data member and three position members (one for x, y and z respectively). A tree with three branches instead of the regular binary tree. The array then, would actually be a function that would create pointers and fill the data like a constructor and return the pointer to the first element and then the library would include referencing functions, making it appear as though there were a class holding my array. Now, to the question. is there a way I can make all these pointers in the function such that they persist past the call to the function? it wouldn't do me much good to have a pointer to some memory space thats been freed after the function called and returned. it would be worse if the space was not free because then I would be lacking the pointers to empty it myself later. One other option I had thought of was to ask whether or not new members could be added during initialization of a class but this seems to not be the case as I think about it. they would just be more variables local to the constructor if I made anything inside it. if none of these things are possible suggestions are welcome but if the answer is simply "no, it doesnt work that way" I'll figure something else out.

回答 (4)

2008-12-08 3:33 pm
✔ 最佳答案
I think you're trying too hard.
In C++, you use dynamic allocation (e.g. using malloc or the "new" keyword) to allocate memory inside functions. though the local pointer might hold them is destroyed, the memory stays allocated, so unless you manage the memory somewhere else, you should manage it inside your class.
The solution for your problem should take into considerations the following factors -
Is the size of the array (or any of its dimensions) fixed after initialization?
What kind of access do you need? (arbitrary/random, iterative along one dimension, a "walk" between adjacent cells, etc.).
For the simplest case, when the array has fixed dimensions, the easiest solution would be to allocate one consecutive block of memory, and convert the triple (x-y-z) index into one linear index.
In two dimensions. Think of a 2-dimension array as a concatenation of 1-dimension arrays, and a 3-dimension array as a concatenation of 2-dimension arrays. The index conversion is fairly easy -
For 2 dimensions - say you have N rows and M columns, and you want to access the 5th column of the 3rd row, the linear index is 3*M+5
I'll let you figure out for yourself how to extend it to 3 dimensions.
2008-12-09 1:34 am
In short, yes. However, in general speaking, you would prefer declaring a container variable OUTSIDE the function, then create the actual object inside. Hope it solves your puzzle, good luck!
2016-10-16 1:04 am
function of hymen The hymen is discovered as a perforated or septate membranous ring around the vaginal commencing or could be discovered blocking off the outlet. The hymen rather has no anatomical function interior the human physique. inspite of the shown fact that, there is an age-old custom that the intact hymen is evidence of virginity interior the female. inspite of the shown fact that, modern technology now could understand that that's an misguided actuality
2008-12-08 3:04 pm
make the pointer a class level variable, or an "instance variable". or include it in the "return" value from the function. Instance variables purpose is to persist and be used between all the functions.

Instance variables should be private and only the methods should have access to them.

(I don't know what language you're using. So I'm sort of 'broad overview'-ing)


收錄日期: 2021-05-01 01:01:52
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20081208063632AAWseWT

檢視 Wayback Machine 備份