Hi,
I want to design a class which is going to hold a set of n+2 numbers:
object(i) = (n-1, 0, 1, 2, 3, ..., n-1, 0)
Later, I want to use that object in another class and have that object accessible from all methods.
If that was an array I would declare a pointer in private section of my class and use new operator in the constructor of the class. But what should I do for this case?
I think that what I want to do is to create a pointer to this class (object) and use new operator to call the NearestNeighbour(ISite i) constructor and delete[] to call the destructor. How can I do that?
Or should I declare this object as static?
Basically, I want this object NearestNeighbour to be created and initialized only once, when I call the constructor of class A (class that uses NearestNeighbour class). But I need to access this NearestNeighbour array from all the methods of class A.
I don't see what the problem is. What have you tried? Have you gotten an error message trying this?
FYI, instead of overloading operator(), I think it is best to overload operator[], since the purpose is to access array elements. Just a matter of preference, I guess.
What I wrote so far is OK, compiles without errors. But how can I change this to do this:
Basically, I want this object NearestNeighbour to be created and initialized only once, when I call the constructor of class A (class that uses NearestNeighbour class). But I need to access this NearestNeighbour array from all the methods of class A.
Sounds to me that Class A is the only one that should have rights to create NearestNeighbour objects (even if only one). Simple: Forward-declare Class A before NearestNeighbour, then make NearestNeighbour's constructor private and make Class A a friend. Now Class A is the only one able to create NearestNeighbour objects.
But than I need to construct NearestNeighbour in every method of class A that I want to use it. I want class A to call the constructor of NearestNeighbour only once, when I instantiate class A.
You don't need to call the constructor every time. Why do you think that? Just construct NearestNeighbour once in the constructor of Class A and store it in a variable. Then use this variable elsewhere.
OK than I don't understand something.
A variable lives only in the scooping region it has been defined, isn't that true?
If I want to have an object accessible in the entire class I have to define it as private:. To do that I have to declare it and give its size explicitly or define a pointer to that object and using new operator make that pointer to point to a given position in RAM where the elements of that array are stored.