Thread: Pointers to Classes || pointers to structures

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    60

    Pointers to Classes || pointers to structures

    What I don't understand is this, "->," symbol. They say it makes stuff point to classes but why don't you just do this? (\/ \/)
    Code:
    int * mypointer = &myclass;
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Presumably myclass is an object of a class, and so you want a pointer to that class type (or a base type), and not a pointer to an int.

    The arrow pointer is equivalent to dereferencing an object pointer (getting the object), and then accessing it with the '.' operator. It is just shorthand. For example:
    Code:
    struct foo
    {
       int bar;
    };
    
    int main( )
    {
       foo* obj = new foo;
       foo->bar = 5; // This is equivalent to...
       (*foo).bar = 5;
       return 0;
    }

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    The arrow operator as it's called allows you to access the member of an object through a pointer. As you know, to access a member of a structure or class you do this:
    Code:
    struct da {
      int member;
    };
    
    struct da s;
    
    s.member = 10; /* Access with <instance>.<member> */
    Now, when you have a pointer to a structure or class, as is more common than you might imagine, you would need to first dereference the pointer and then use the dot operator to access the member:
    Code:
    struct da s;
    struct da *p = &s;
    
    (*p).member = 10; /* Very awkward */
    The arrow operator is a syntactic convenience that allows you to make that clumsy dereference/member-access into something simple and elegant:
    Code:
    p->member = 10;
    This is all the arrow operator does.

  4. #4
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    The -> is also used in polymorphism.
    say you have base class Point and derived class Circle, and they both have their own print functions as virtual void print();

    Code:
    Point point();
    Point *pointPtr = 0;
    
    Circle circle();
    Circle *circlePtr = 0;
    
    point.print() // static binding, uses base class point print function
    circle.print() // static binding, uses derived class circle print function
    
    pointPtr = &circle;
    pointPtr ->print(); //dynamic binding..base class pointPtr invokes derived class circle's print function.
    big146

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    If you follow that line of thinking then the dot operator is used in polymorphism as well:
    Code:
    #include <iostream>
    
    struct a {
      virtual void print() { std::cout<<"a"<<std::endl; }
    };
    
    struct b: public a {
      void print() { std::cout<<"b"<<std::endl; }
    };
    
    int main()
    {
      a ao;
      b bo;
    
      a& ra = ao;
      a& rb = bo;
    
      ra.print();
      rb.print();
    }
    However, despite that, the fact remains that even with polymorphism, the arrow operator is still dereferencing an object and accessing a member. So you are not referring to an overload of the operator as your wording implies.

  6. #6
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    Declaring a member function virtual enables the program to determine which function to invoke based on the type of object to which the handle points, rather than on the type of the handle.

    ra.print() // static binding(compile time)
    ra->print() //dynamic biding( runtime) polymorphism..decides at runtime which object function print to call.
    Last edited by big146; 07-28-2004 at 06:37 AM.
    big146

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The dot operator does not necessarily imply static binding. Here is an example program to demonstrate what you are talking about with polymorphism (which really is independent of the operators mentioned):
    Code:
    #include <iostream>
    
    struct A
    {
       virtual void f() { std::cout << "A\n"; }
    };
    
    struct B : public A
    {
       void f() { std::cout << "B\n"; }
    };
    
    int main()
    {
       A* obj = new B;
       obj->f();
       (*obj).f();
    
       B obj2;
       A& ref = obj2;
       ref.f();
    
       return 0;
    }

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    Could anyone give me an explanation of what the "->" operator with commands I actually learned? I have no clue what the print() function does (I know what printf() does) nor what a foo variable is nor what a virtual void return type is (all I know is I hate that pesky four letter word) and you don't need to explain to me what the "." operator does because that's simple.
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Basically, the "." is used to access member variables and methods of a non-pointer, and the "->" is the same thing, but is used with pointers instead. So if you understand the ".", and you understand what a pointer is, then you understand the "->".

    Look again at Zach L.'s example in the second post of the thread. If you are still having trouble, maybe you can give some simple code that uses the ".", and we can change it so that it uses the "->".

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    So is this using correct syntax? :
    file1.cpp
    PHP Code:
    //This is C++ code, not PHP. I like PHP code tags since it makes me a little more organised.
    #include<iostream.h> //////////////I don't use iostream, I'm a fan of iostream.h :)
    #include<string.h>
    #include<conio.h>
    using std::string;
    struct mystruct
    {
      
    int pointer;
    };

    int main()
    {
      
    string hi "Hi!";
      
    mystruct->pointer = &hi////////Why didn't Bjorne Stroustrap make the "." operator for pointers too?
      
    cout << *hi;
      
    cout.flush()
      
    getch();
      return 
    0;

    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  11. #11
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    no your going to want to do something like this
    Code:
    #include <iostream>
    using std::cout;
    class loser
    {
    public:   
     int loserPower;
    }
    
    int main()
    {
       loser brian;
       loser *pBrian;
       pBrian=&brian;
       pBrian->loserPower=10;
       cout<<brian.loserPower;
       cout<<pBrian->loserPower;
    }
    btw: <iostream.h> is old <iostream> is new and they both do the same thing so if you like one, you like the other
    Last edited by prog-bman; 07-28-2004 at 07:31 PM.
    Woop?

  12. #12
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I tried to use your setup to show the simmilarities and differences between "." and "->".
    PHP Code:
    #include<iostream.h>
    #include<conio.h>

    struct mystruct
    {
      
    int value;
    };

    int main()
    {
      
    // Create an instance of mystruct - name the variable "instanceOfMyStruct"
      
    mystruct instanceOfMyStruct;

      
    // Use "." to access the member variable like you already understand (I hope).
      
    instanceOfMyStruct.value 5;

      
    // Create a pointer that can point to any instance of mystruct.
      
    mystructpointerToMyStruct;

      
    // Set the pointer to point to the instanceOfMyStruct.
      
    pointerToMyStruct = &instanceOfMyStruct;

      
    // Use "->" like we used "." above, to access the member variable of an
      // instance of mystruct.
      
    pointerToMyStruct->value 10;

      
    // Output the value using the instance or the pointer.
      // Both should output the same number (10) because the
      // pointer simply points to the instance.
      
    cout << "Instance value using ".": ";
      
    cout << instanceOfMyStruct.value << endl;
      
    cout << "Pointer value using "->": ";
      
    cout << pointerToMyStruct->value << endl;

      
    getch();
      return 
    0;


  13. #13
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    Quote Originally Posted by prog-bman
    btw: <iostream.h> is old <iostream> is new and they both do the same thing so if you like one, you like the other
    nope, not true. In <iostream> you have to put the namespace std before mostly everthing that require a namespace. In iostream.h, u don't.
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  14. #14
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Really i didn't know that thanks for telling me
    They both accomplish the same thing <iostream> is now the standard so if you want to use old headers thats your call but how hard is it to put using namespace std; at the top of your c++ files or using std::whatever
    Woop?

  15. #15
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    Quote Originally Posted by prog-bman
    pBrian->loserPower=10;
    And tell me why u would use the -> operator there? In class loser u set loserPower as an integer. U said u only use the -> operator when the > side of -> is a pointer! Ur confusing me.
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using getc(stdin) or getchar(c)
    By daedenilus in forum C Programming
    Replies: 11
    Last Post: 11-13-2005, 01:00 AM
  2. how to extract words from a string
    By panfilero in forum C Programming
    Replies: 7
    Last Post: 11-04-2005, 08:06 AM
  3. Error
    By Kaho in forum C Programming
    Replies: 5
    Last Post: 08-19-2005, 01:56 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. Freeing pointers in structures
    By jim50498 in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 12:53 PM