Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 02-08-2005, 02:24 AM   #1 (permalink)
abs
Registered User
 
Join Date: Jan 2005
Posts: 7
abs is on a distinguished road
Class using a specific instance of another class? is this possible?

Hello! I'm trying to write a program which will take in a list of input files. Then create an instance of Class A per input file. Functions will later on create new instances of Class C. But the difficult part is that ClassC must be able to access one of the members(a lookup table) of a specific classA object.

Code:
//main.cc
vector<A*> list_of_As;

some_function{
for ( list of file names)
	{
	A * temp;
	temp = new A;
	do_something(temp);
	list_of_As.push_back(temp);
       }

}

do_something(A * temp)
{
C * temp;
temp = new C(A);
//Class C object then uses member variable of the class A object the loop is currently on.  Not just any class A object! THIS specific one.

...

}
//--

//a.cc
class A
{
public:
A(){}
vector < string > Lookup_Table; //Justa an example, chose a random data structure.
};
//--

//others.cc
class A; //Forward declaration?

//Abstract base class for numerous derived classes. (later on)
class B
{
public:
B(){}

// This constructor will be called by derived class.
B(A * temp)
{
*Lookup_Table = temp->Lookup_Table; //Set this base classes' variable, so the calling derived class's will have access to class A's Lookup_Table 
}

void base_function()=0;

vector<string> * Lookup_Table;
int members;
}

class C: public B;
{
public:
c(){}

C(A * temp):B(temp){} //Here we pass the A* to the base class's constructor which will point our Lookup_Table to that of this specifc class A  object's.

void base_function()
{
Lookup_Table ...  do something with it.
//Important thing is that we are now accessing the member variable from class A!!
}

}
//--
eh... i dunno if i made it clear enough. I'm doing this because I need to create multiple lookuptables, one per file. Each lookup table will be different depending on what is in the file. But setting the lookuptable is done elsewhere... right now in class B! How can i do this...? maybe a bad design...?
thanks for any insight...
abs is offline   Reply With Quote
Old 02-08-2005, 08:27 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
It is very hard to see what you're up to. So I can't make a proper design. Below is a swing in the air regarding your intentions. See if you can learn the things you want to learn:
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)
  {
    Base_LookupTable_.push_back("Base1");
    Base_LookupTable_.push_back("Base2");
    Base_LookupTable_.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 Base/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;
  }
  
  return 0;
}
__________________
Valmont is offline   Reply With Quote
Old 02-08-2005, 12:17 PM   #3 (permalink)
abs
Registered User
 
Join Date: Jan 2005
Posts: 7
abs is on a distinguished road
pretty close.. but when you print right you get :
//from base/derived
A1
A2
A3
Base1
Base2
Base3
Derived1
Derived2
Derived3

//from A
A1
A2
A3

Is there any way so that the base/derived class modifies the actual table from A? so the output will be:

//from base/derived
A1
A2
A3
Base1
Base2
Base3
Derived1
Derived2
Derived3

//from A
A1
A2
A3
Base1
Base2
Base3
Derived1
Derived2
Derived3
abs is offline   Reply With Quote
Old 02-08-2005, 02:15 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
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 03:31 PM.
Valmont is offline   Reply With Quote
Old 02-08-2005, 04:04 PM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Although I think you mean something like this:
Code:
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>

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

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

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

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

class Base
{
public:
  Base() {}
  Base( A& a ) : theA(a)
  {
     theA.model_lookupTable().push_back("Base") ;
  }
  virtual void some_function()=0;
  const vector<string>& get_table()
  {
    return theA.model_lookupTable();
  }
protected:
  A theA;
};

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

class Derived : public Base
{
  public:
    Derived(){}
    Derived(A& a) : Base(a) {}

    void some_function()
    {
      theA.model_lookupTable().push_back("Derived");
    }
};

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

int main()
{
  A myA;
  //Modfiy property directly.
  myA.model_lookupTable().push_back("A");
  //Constructor of class Base will modify property of class A.
  Base* myInstance = new Derived( myA );
  //Constructor of class Derived will finally modify property of class A.
  myInstance->some_function();

  //So what do we got for myInstance?
  for(unsigned i =  0; i < myInstance->get_table().size(); ++i)
  {
    cout<<"From instance: "<< myInstance->get_table()[i]<<endl;
  }
  
  //And class A?
  for(unsigned i =  0; i < myA.model_lookupTable().size(); ++i)
  {
    cout<<"Class A: "<< myA.model_lookupTable()[i]<<endl;
  }
  
  delete myInstance;
  return 0;
}
__________________
Valmont is offline   Reply With Quote
Old 02-08-2005, 04:12 PM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Or we can let Base/Derived manage class A on their own:

Code:
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>

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

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

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

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

class Base
{
public:
  Base() : theA(new A)
  {
     theA->model_lookupTable().push_back("Base") ;
  }
  ~Base()
  {
    delete theA;
  }
  //
  virtual void some_function()=0;
  //
  const vector<string>& get_table()
  {
    return theA->model_lookupTable();
  }
protected:
  A* theA;
};

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

class Derived : public Base
{
  public:
    Derived(){}
    void some_function()
    {
      theA->model_lookupTable().push_back("Derived");
    }
};

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

int main()
{
  //1) Constructor of class Base will create an instance of class A.
  //2) Then it will modify the property of class A as a demo.
  Base* myInstance = new Derived;
  //Constructor of class Derived will modify property of class A. Class A's
  //instance is created by class Base.
  myInstance->some_function();

  //So what do we got for myInstance?
  for(unsigned i =  0; i < myInstance->get_table().size(); ++i)
  {
    cout<<"From instance: "<< myInstance->get_table()[i]<<endl;
  }
  
  delete myInstance;
  //A's instance cease to exist after deletion of myInstance.
  return 0;
}
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
someone please help kickerman97 Java 3 10-19-2004 04:19 PM
to put data methods inside class or not? sde Java 2 05-25-2004 05:09 PM
class theory sde PHP 2 01-11-2003 01:48 PM


All times are GMT -8. The time now is 04:55 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting