Thread: Is it possible to type-cast an 'access variable' so it could.....

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    57

    Is it possible to type-cast an 'access variable' so it could.....

    ..be compatible with another class?

    By the way, 'access variable' is the variable used to access the a class. Heres what I mean:

    [code]
    #include <iostream>

    class Database
    {
    public:
    int x;
    }Data;

    class Storage
    {
    public:
    int y;
    }Stor;

    int main(int argc, char *argv[])
    {
    Storage *access;
    access= (Database&)Stor; // Is this legal?
    // If not, is there another way of pulling this stunt?
    access.x= 9;

    cin.get();
    return 0;
    }
    "A Programmer being told to 'go to' hell sees the 'go to' part of the sentence as the worst part." - Master Foto

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    ignoring the problems with your syntax, yeah its legal... although in most cases it wont give you the desired result. You might be able to use a void pointer and just cast the pointer when you want to use it.

    void *ptr = (void *)&something;
    ((something *)ptr)->method1();

    kinda ugly, but it will probably work...
    However, inheritance and polymorphism might be better for what you want

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    13
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  3. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  4. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM