I have a class that looks like this

//the mem_data class======================================//
class mem_data { 

 public:
  mem_data(int,int,std::string);
  ~mem_data();

 private:
  std::vector<reader> mem_store;
  int complete;

  friend class model_base;

};

Now, I would like to let the class model_base access mem_store

My main code will contain the following:

mem_data test_data(20110601,10,"test_file.csv");

 model_base test_model();

I basically want test_model to have access to mem_store of test_data. How can I go about writing model_base to accomplish this?

So far, I have

//the MODEL_BASE class======================================//
class model_base { 

 public:
  model_base();
  ~model_base();

 private:
  mem_data *pass;

};

I want to somehow pass the address of test_data into this new class so that I can do something like

pass->mem_store.at(0);

Can anybody show me how to accomplish this?

If you have to give another class access to private variables, you can have a member function that returns a pointer or reference to the variable:

class X
{
   public:
      double &get();

   private:
      double y;
};

This gives control over which private variables are exposed, so I find it preferable to making the second class a friend.

Can somebody show how this can be done using friend classes like I was attempting above?

Actually, I think I found a working solution...

//the model_base class======================================//
model_base::model_base(mem_data &mem_block) {
  pass=&mem_block;
}

reader model_base::get(int date) {
  return pass->mem_store.at(0);
}

model_base::~model_base() { }

Basically, I pass the mem_data object test_data as a reference when the new class is instantiated and I set its address to the pointer pass. Then, I can simply use pass-> when I need to access the object later on in the new class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.