Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6647381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:31:51+00:00 2026-05-26T00:31:51+00:00

i am developing a project in which i have used many classes. for creating

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:31:52+00:00Added an answer on May 26, 2026 at 12:31 am

    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.

    class banana
    {
      apple apple_;  // a banana HAS A apple
    };
    

    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 BananaRepository which is responsible for storing and retrieving bananas.

    class BananaRepository
    {
      typedef XYZType BananaID;
      Banana& GetByID(BananaID  id);
      BananaID Add(Banana& b);
    };
    

    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.

    class BananaRepository
    {
    public:
      typedef std::vector<Banana>::size_type BananaID;
    
      Banana& GetByID(BananaID  id)
      {
        return bananas_.at(id);
      }
    
      BananaID Add(Banana& b)
      {
         bananas_.push_back(b);
         return bananas_.size() - 1;
      }
    
    private:
      std::vector<Banana> bananas_;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a small POS for a university project. I have a form which
I'm starting a new project which involves developing an interface for a machine that
In developing a large C++ programming project with many developers, we have run into
I am developing an iphone project in which I have to use Walrus S3
Which pattern could be used in web projects? I am developing a wiki project
I have an open source project which I had been actively developing JStock -
I'm currently developing on a project which uses some TimerJobs. One of the jobs
I am going to be developing a large project which will include a large
Our team is developing a rather big ASP.NET web project which initially started in
We are developing customized installer using visual studio 2008 installer project. The requirement which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.