why use the -> operator?

This is a discussion on why use the -> operator? within the C++ Programming forums, part of the General Programming Boards category; I was just wondering why, when using object pointers, you must use the -> operatior. Why couldnīt you simply follow ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    40

    why use the -> operator?

    I was just wondering why, when using object pointers, you must use the -> operatior. Why couldnīt you simply follow the standard method like: *p.funct() if you were accesing the funct() member through the *p pointer?

    Thanks for the help

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,705
    sure, you can do that - but because of operator precedence you'll need to put a parenthesis around the pointer dereference, ie: (*p).funct()
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    And it's a lot easier to just write -> than to write all the parentheses and *'s, let alone site there and figure out where they're all supposed to go.

  4. #4
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    For example sake, let's create a structure called point:
    Code:
    struct point {
    	int x;
    	int y;
    };
    The following declaration says that p is a pointer to a structure of type struct point. If p points to a point structure, *p is the structure, and (*p).x and (*p).y are the members:
    Code:
    struct point *p;
    To use p, we might write, for example:
    Code:
    struct point *p;
    
    printf("(%d %d)\n", (*p).x, (*p).y);
    The parenthesis are necessary in (*p).x because the precedence of the structure member operator (.) is higher than (*). The expression *p.x means *(p).x, which is illegal here because (x) is not a pointer.

    Pointers to structures are so frequently used that an alternative notation is provided as a shorthand. If (p) is a pointer to a structure, then the following code refers to the particular member:
    (p)->member-of-structure

    Note: Both (.) and (->) associate from left to right.

    Edit: Takes me forever to post. Heh.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Also, when you get into operator overloading, * and -> often will not mean "dereference" any more; rather, they will mean "dereference one of my member variables", in which case I personally find -> to be more intuitive - though I imagine that's totally up to you.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Overloading -> operator
    By Hunter2 in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2004, 03:08 PM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. -> operator and its uses and benefits
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2002, 02:56 AM

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