hello,
i am developing a project in which i have used many classes.
for creating classes i have used the new operator...example, in banana class i have an instance variable of class apples......
in header file (banana.h):
1 2 3 4 5 6
|
static int counter = 0;
class banana
{
public:
apples *ap_obj;//(apple is a class defined another file apples.cpp)
int *index;
|
in banana.cpp :
1 2 3 4 5 6 7 8 9
|
class banana
{
banana::banana(void)
{
ap_obj = new apples;
index = new int;
*index = ++counter;
}
};
|
my first question is that, is my method correct (there is no run time error) in terms of memory efficiency?
my second question is that, i want to access another banana object in 1 of my methods of banana by using the index (plz note that every banana object has a unique index) variable.
for this i am thinking of using another class registry(because i want to store indexes of many classes's objects).
i am thinking of storing the pointer of 1st object of any class in my registry class.
& for accessing the pointer of any nth object of a class, i plan to use pointer arithmatics on the 1st object using the index variable...example
1 2 3 4 5 6 7 8
|
class registry
{
banana *base_obj;//this value will be initialised when i create the 1st object of banana class
banana *registry::get_nth_object(int shift);
{
return *(base_obj + shift);//shift is the index variable of banana class
}
};
|
i want to use this get_nth_object in any class & do not want to go out of scope...hence the use of pointers
hope you people will realise this.
if there is any other efficient way of doing this by saving memory & run 'time'....then plz do tell me.
thanks a lot for reading my post :)