Getting class info through a pointer

This is a discussion on Getting class info through a pointer within the C++ Programming forums, part of the General Programming Boards category; I have a pointer that points to a class. How do I get class members through the pointer? example: Code: ...

  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,796
    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
    University of Waterloo
    Posts
    1,904
    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, 05:23 AM
  2. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 04: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, 10:25 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21