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……
THIS IS A SCALED DOWN REPRESENTATION OF WHAT I WANT TO SAY & DOES NOT REPRESENT THE CODE WORD-TO-WORD…SO PLEASE DO NOT POINT AT SYNTAX ERRORS…BUT TRY TO UNDERSTAND THE METHODOLOGY
in header file (banana.h):
static int counter = 0;
class banana
{
public:
apples *ap_obj;//(apple is a class defined another file apples.cpp)
int *index;
}
in banana.cpp :
class banana
{
banana::banana(void)
{
ap_obj = new apples;
index = new int;
*index = ++counter;
}
};
my first question is that, is my method correct in terms of memory efficiency?
( i know that i dont have any run time error for sure)
my second question is that, i want to access a banana object in 1 of my methods of any class 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
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
}
};
in any other class i can just call get_nth_object & pass the index no of the object whose pointer i want & i will get that point.
is there anything wrong in my code?
or if anything can be improved please help me.
for refernce i use http://www.cplusplus.com.
i am a mech engg student so please pardon me if i have made any stupid mistake
First you should find out what kind of relatioship your banana->apple relation is. If it is a composition (A banana is composed of its apple and…) the simplest think is to store an apple instance in a banana.
If this is true, but you have myriads of bananas and there are a number of apples that a equal you could implement the Flyweight Pattern to safe memory usage. But you shouldn’t do this because you THINK you have a memory problem, but because you MEASURED that you have a memory problem due to apples.
If your relationship is more a shared ownership, you should store a pointer to the apple instance in banana and think about where to store the apple objects. The easiest way would be to store
std::shared_ptr<apple>and let the reference counter control the apple lifetime.You’re second question was how to store and access banana Objects. I would introduce a
BananaRepositorywhich is responsible for storing and retrieving bananas.You should abstract the kind of used ID and the storage, so client code does not depend on that details. Each banana has a ID and i can get a banana from the repository using this id. The easiest implementation would be with banana::ID = vector::size_type and a vector in BananaRepository.