overloading pointer operator

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?
What do you mean by "object(i) = (n-1, 0, 1, 2, 3, ..., n-1, 0)"? You want an array that its last two elements and first two elements are the same?
Here is what I have now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class NearestNeighbour
{
typedef int ISite;
public:
NearestNeighbour()
{
	_nn = new ISite[3];
	_nn[0] = _nn[1] = _nn[2] = 0;
}

NearestNeighbour(ISite i)
{
_nn = new ISite[i + 2];
for (ISite j = 1; j <= i; ++j)
	_nn[j] = j - 1;
_nn[0] = i - 1;
_nn[i + 1] = 0;
}

~NearestNeighbour()
{
delete[] _nn;
}

inline ISite operator()(ISite i)
{
	return this->_nn[1 + i];
}

private:
	ISite *_nn;
};
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.
Last edited on
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.
Last edited on
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.
Last edited on
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.

How can I do what webJose wrote?
Last edited on
1
2
3
4
5
6
7
8
9
class A
{
private:
    NearestNeighbour n;

public:
    A(NearestNeighbour::ISite numElements) : n(numElements)
    { }
};


Just like that. Every time you need to, you just use 'n'.
Topic archived. No new replies allowed.