View Single Post
Old 02-08-2005, 01:15 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Well we are passing an vector of an instance of class A to class Base. So use that vector to modify it:
Code:
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;

//------------------------------------------------

class A
{
public:
  A(){}
  vector<string>& model_lookupTable()
  {
    return ModelLookupTable_;
  }
private:
  vector <string> ModelLookupTable_;
};

//------------------------------------------------

class Base
{
public:
  Base() {}
  Base( vector<string>& lt ) : Base_LookupTable_(lt)
  {
    lt.push_back("Base1");
    lt.push_back("Base2");
    lt.push_back("Base3");
  }
  //
  vector<string> base_lookupTable() const
  {
    return Base_LookupTable_;
  }
  //
  virtual void some_function()=0;
protected:
  vector<string> Base_LookupTable_;
  int members;
};

//------------------------------------------------

class Derived : public Base
{
  public:
    Derived(){}
    Derived(vector<string>& lt) :
      Base(lt)
    {}

    //Lookup_Table ...  do something with it.
    //Important thing is that we are now accessing the member from class A!!
    void some_function()
    {
      Base_LookupTable_.push_back("Derived1");
      Base_LookupTable_.push_back("Derived2");
      Base_LookupTable_.push_back("Derived3");
    }
};

//------------------------------------------------

int main()
{
  //Let us create a vector.
  A myA;
  myA.model_lookupTable().push_back("A1");
  myA.model_lookupTable().push_back("A2");
  myA.model_lookupTable().push_back("A3");

  //The vecor created (above) will be modified by class Base constructor.
  Base* myInstance = new Derived( myA.model_lookupTable() );
  //And then the vector will be modified by class Derived method.
  myInstance->some_function();

  //What has the Derived class in store?
  for(std::size_t i = 0; i < myInstance->base_lookupTable().size(); ++i)
  {
    cout<<myInstance->base_lookupTable()[i]<<endl;
  }
  cout<<endl;
  //What has classs A in store?
  for(std::size_t i = 0; i < myA.model_lookupTable().size(); ++i)
  {
    cout<<myA.model_lookupTable()[i]<<endl;
  }
  
  system("pause");

  return 0;
}
The options are endless. You should be more clear on what exactly you want. Perhaps you want to modify the properties of a certain instance of class A.
Suppose the base class is doing that, then write a method where it accepts a class A instance parameter. Then use that passed value to modify A's contents:
Code:
void Base::Base(A& theA)
{
   theA.someVector[0] = "Index 0 has been modified.";
}
Really there are unlimited options but I have no clue wich one is the right one for you.

Or perhaps you want to compose class Base of class A:
Code:
class Base
{
public:
   Base() : theA(0) {}
   Base(A* a) :  theA(a) 
   {
       //Do stuff with theA->...etc
   }
private:
   A* theA;
}
Etcetera, etcetera, etcetera...

You need to be precise. Perhaps we don't need class A at all. Or perhaps we don't need to encapsulate a vector in class Base (or derived) at all! Right now I can't say.
__________________

Last edited by Valmont; 02-08-2005 at 02:31 PM.
Valmont is offline   Reply With Quote