Thread: pointer to an instance of a different class

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    2

    pointer to an instance of a different class

    This is what I have in hand as of now:
    class classA
    {
    public:
    int classtype;
    int dummy;
    void * pObj;
    };

    class classB
    {
    public:
    int classtype;
    int dummy;
    };

    classA iclassA[20];
    classB iclassB[20];


    int main()
    {
    iclassA[0].pObj = (void *) &iclassB[0];
    iclassB[0].dummy=5;
    system("PAUSE");
    return 0;
    }

    Here void * pObj is an attribute of classA that points to an instance of classB namely iclassB[0]. What I would like to know is how I can assign the value of an attribute of the instance iclassB[0] to an attribute of the instance iclassA[0] that holds pObj.

    Shall appreciate all input.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    Code:
    class classB;
    class classA
       {
       public:
          int classtype;
          int dummy;
          classB * pObj;
       };
    
    class classB
       {
       public:
          int classtype;
          int dummy;
       };
    
    classA iclassA[20];
    classB iclassB[20];
    
    
    int main()
       {
       iclassA[0].pObj = &iclassB[0];
       iclassB[0].dummy=5;
       system("PAUSE");
       return 0;
       }
    is that what you mean?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Just a new , your post brought up a glaring question to me. In class a you have void * pObj, and in main you typecast a refrence to a classB object to void. What is a pointer to void? Points to nothing? Why don't you use classB * pObj in your classA declaration instead and avoid the typecast? Just a bit confused, then couldn't you just create a pointer in class B say classB * bObj=classA.pObj?

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    2
    FillYourBrain and curlious, thank you for your input!
    What I am still not clear about though is, given that an instance of class now has an attribute in the form of pObj that is of type classB *, how can this be used to assign values of attributes of the instance of classB to other attributes of the the instance of classA. That is, I have declared that iclassB[0].dummy=5. How can i assign the value that is help by this attribute to the "dummy" of iclassA[0] using pObj ??

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    iclassA[0].dummy = iclassA[0].pObj->dummy;

    Is that what you want?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer of array of class
    By 11moshiko11 in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2008, 09:58 AM
  2. Which one is "quicker" ?
    By AloneInTheDark in forum C# Programming
    Replies: 2
    Last Post: 02-08-2008, 09:23 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. A class pointer to the class I come from
    By Lumber606 in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2003, 06:46 PM