Thread: Getting class info through a pointer

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    35

    Getting class info through a pointer

    I have a pointer that points to a class. How do I get class members through the pointer?

    example:
    Code:
    class TestClass
    {
    public:
              int Num;
    };
    
    void main()
    {
              TestClass TC;
              TC.Num = 5;
              TestClass *TCPointer;
              TCPointer = TC;
              
              cout << TCPointer.Num;
    }
    Problem is, I can't have TCPointer.Num - I can only have TCPointer. As I write this, I found I way I don't need to do it anymore, but is this possible to do?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You assign the address of an object with the address-of operator. You can then access the members of that object through the pointer with the arrow operator:
    Code:
    class TestClass
    {
    public:
              int Num;
    };
    
    int main()
    {
              TestClass TC;
              TC.Num = 5;
              TestClass *TCPointer;
              TCPointer = &TC; // Address-of operator
              
              cout << TCPointer->Num; // Arrow operator
    }
    My best code is written with the delete key.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Code:
    class TestClass
    {
    public:
    int Num;
    };
     
    void main()
    {
    TestClass TC;
    TC.Num = 5;
    TestClass *TCPointer;
    TCPointer = TC;
     
    cout << TCPointer.Num;
    }
    use TCPointer->Num;

    and to assign TC to TCPointer, you need to do: TCPointer=&TC

    -edit-
    meh, prelude you beat me, i had to eat my soop!!

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. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. class passing a pointer to self to other class
    By daioyayubi in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2005, 09:25 AM