"->" and "?" operators

This is a discussion on "->" and "?" operators within the C++ Programming forums, part of the General Programming Boards category; Hi. Can anyone help give info on the "->" and "?" ? I see them a lot in codes and ...

  1. #1
    Unregistered
    Guest

    Unhappy "->" and "?" operators

    Hi. Can anyone help give info on the "->" and "?" ?
    I see them a lot in codes and don't have a clue to what they mean.

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    I would like info on the ? operator too. It still remains a part of coding I don't understand =)

    However, the -> operator is a pointer to an item in a class/struct. Such as

    BITMAP* mybit;
    return (mybit->bmWidthBytes);

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    If you have

    a = b != c ? d : e;

    Its the short hand for

    if ( b != c ) {
      a = d;
    } else {
      a = e;
    }

  4. #4
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Unhappy lalala...

    I think that explination was a bit harsh...

    the ? operator is like the if operator, it takes a boolean variable, and does something if its true / false. The difference here is this works more like the #define thing becouse you can use it anywere:

    bool lala = true;

    printf(lala ? "IT WAS TRUE!" : "IT WAS FALSE!");

    or:

    bool lala = true;
    lala ? dothistrue() : dothisfalse();

    About the '->' operator, its just like '.', exept for pointers:

    struct blah
    {
    int lala;
    };

    blah *i = new blah;
    i->blah = 6;
    delete i;

    Hope this was helpfull, SPH

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    -> would be the indirect equivalent of something.something in Java,

  6. #6
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Exclamation Not really...

    You can do structures/classes jsut the same in C/C++:

    struct blah
    {
    int h;
    };

    blah i;
    i.h = 6;

    '->' is for POINTERS to structures/classes.

    SPH

  7. #7
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    the arrow operator is for dereferencing conglomerate user-defined types. they can be unions, struct, or classes. passing a pointer to an instance of a userdefined type as an argument to a function helps because it is less expensive on the stack when you call the function. deferencing it allows programmers a handier way to access the data members [and/or member functions] of the conglomerate data type, other than to use standard dereferencing using the asterisk. also, using the arrow operator ensures that correct deferencing is ensured, though the size of the user defined type is unknown. it's members will be correctly retrieved.
    hasafraggin shizigishin oppashigger...

  8. #8
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    "'->' is for POINTERS to structures/classes."

    I thought -> was used as a pointer to an item in a structure/class..?

  9. #9
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >I thought -> was used as a pointer to an item in a >structure/class..?

    That's correct. Assume S is a structure which has an item called N. Let iS be a variable of type S, then N can be used as iS.N. Now assume pS is a pointer to S, then N can be used as pS->N.

    So:

    Code:
    struct blah 
    { 
    int h; 
    }; 
    
    blah i; 
    blah *p;
    
    i.h = 6; 
    p->h = 6;

  10. #10
    Unregistered
    Guest
    >I thought -> was used as a pointer to an item in a >structure/class..?

    That's correct.


    No it's not!!! if you have a pointer to an structure or class, then you use the -> operator to 'dereference' the pointer, and access a member that is part of the structure

    if you have this:

    typedef struct _p
    {
    int a;
    } P;

    and:

    P p;
    P* pp = &p;

    then:

    pp->a = 10;

    does the same as

    (*pp).a = 10;

    it's just friendlier notation...

    -> was used as a pointer to an item
    is wrong... that operator is used to reference an item within the structure... it's not a pointer to the item.

    U.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use of "?" ":"
    By MK27 in forum C Programming
    Replies: 6
    Last Post: 12-03-2008, 02:21 AM
  2. "*x + *x++" Address/Pointer "?"
    By Marth_01 in forum C Programming
    Replies: 10
    Last Post: 11-05-2008, 03:33 AM
  3. What the heck is "?" ???
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-10-2005, 09:58 AM
  4. "?" and ":"
    By EvBladeRunnervE in forum C Programming
    Replies: 5
    Last Post: 02-03-2003, 11:13 PM

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