Like *ClownPimp* sez, you should probably use polymorphism by Deriving the two classes from the same Base class.
Because of this a pointer to the base class can then refer to either object and call methods of that same name in either that are in the base class or overriden in either class.
Here is an example or what polymophism can do for you:
Code:
#include <iostream>
using namespace std;
//
// Really poor class definitions without .h files and so on
//
//--------------------
// Base
//--------------------
class Base
{
public:
Base(){}
// overridables
virtual ~Base(){}
virtual int GetX()
{
cout << "Base GetX()" << endl;
return x;
}
virtual void SetX(int newx) { x = newx;}
protected: // must be at least protected
int x;
}B;
//-------------------------
// DerivedA
//-------------------------
class DerivedA : public Base
{
public:
DerivedA(){}
~DerivedA(){}
int GetX()
{
cout << "DerivedA GetX()" << endl;
return x + 1;
}
}DA;
// A function that calls cout << b_ptr->GetX() << endl;
void PrintX(Base * b_ptr);
//-----------------------
// DerivedB
//-----------------------
class DerivedB : public Base
{
public:
DerivedB(){}
~DerivedB(){}
int GetX()
{
cout << "DerivedB: GetX()" << endl;
return x - 1;
}
}DB;
//------------------
// Main
//------------------
int main()
{
Base * b_ptr;
B.SetX(10); // Should come back as 10
DB.SetX(10); // Should come back as 11
DA.SetX(10); // Should come back as 9
//Point to B
b_ptr = (Base *)&B;
PrintX(b_ptr); // 10
// Point to DA
b_ptr = (DerivedA *)&DA;
PrintX(b_ptr); // 11
// Point to DB
b_ptr = (DerivedB *)&DB;
PrintX(b_ptr); // 9
return 0;
}
// calls GetX() on pBase, but because each Derived class
// overrides it, you get different results based on each
// class, but can switch back for between each when needed
void PrintX(Base * pBase)
{
cout << pBase->GetX() << endl;
}