Thread: -> Operator

  1. #1
    Trying to Learn C nathanpc's Avatar
    Join Date
    Jul 2009
    Location
    Brazil
    Posts
    72

    Red face -> Operator

    Hello,
    I know that this could seen as an extreme beginner question, but I haven't learned this on the book "C Programming Language", as everyone is here to learn and to help, then I think that people should answer nice and very helpful. Anyway, I don't know if that is an operator, but I want to know what is the use of it in a source, as on this example:
    Code:
    something->someFunction();
    Also, I want to know in which cases it's most used, what types of object I can use on the right and on the left side of the expression.

    Remember to be a gently person when answering(because I think this question is too noob that people can be a bit ignorant when answering), also that everyone is here to learn and be respected.

    Best Regards,
    Nathan Paulino Campos
    Follow Me At Twitter
    Eee PC 904HD White | Windows XP Home Edition and Linux Ubuntu Hardy Herron

    Google Talk: eeepc904@gmail.com
    ICQ: 424738586
    AIM: nathanjava

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    as everyone is here to learn and to help, then I think that people should answer nice and very helpful
    Remember to be a gently person when answering(because I think this question is too noob that people can be a bit ignorant when answering), also that everyone is here to learn and be respected.
    This alone makes me want to bully you...

    what is the use of it in a source, as on this example
    It's a convenience operator for accessing a structure member through a pointer to a structure:
    Code:
    struct foo {
      int x;
    } *p;
    Assuming the pointer is initialized, these two statements are equivalent:
    Code:
    p->x = 123;
    (*p).x = 123;
    My best code is written with the delete key.

  3. #3
    Trying to Learn C nathanpc's Avatar
    Join Date
    Jul 2009
    Location
    Brazil
    Posts
    72
    Thanks very much, now I've understood.

    I've liked the second one because it's a little bit object-oriented.
    Last edited by nathanpc; 06-15-2010 at 07:54 PM.
    Follow Me At Twitter
    Eee PC 904HD White | Windows XP Home Edition and Linux Ubuntu Hardy Herron

    Google Talk: eeepc904@gmail.com
    ICQ: 424738586
    AIM: nathanjava

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    in a bad way though

    should be using getters and setters for that kind of stuff

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by LordPc View Post
    in a bad way though

    should be using getters and setters for that kind of stuff
    That doesn't make sense, you'll still have to dereference the pointer.

    i.e.

    Code:
    const char * getName(struct foo * f)
    {
       return f->name;
    }
    I don't see how "getters" nor "setters" let you avoid "->".

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    he said he prefers the second way cause its like oo.

    Code:
       p->x = 123;
       (*p).x = 123;
    i took this as meaning that he likes dot notation. except, in oo, you dont refer to variables through dots, you use getters and setters. so that kind of code should never pop up in oo which is why i said "in a bad way" cause it means he might be writing poor oo code...

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That doesn't even make sense. You still have to write the getter/setter, so you still end up referring to the variables directly at some point. They don't write themselves you know.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    Quote Originally Posted by quzah View Post
    That doesn't even make sense. You still have to write the getter/setter, so you still end up referring to the variables directly at some point. They don't write themselves you know.


    Quzah.
    well ofcourse you do, in the getter and setter. nowhere else though, thats what the getter/setter is for

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're still not gaining anything.
    Code:
    x = foo.bar;
    Isn't any harder to write than:
    Code:
    x = getbar( foo );

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    im talking about oo programming. im not sure you are...

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    He's trying to tell you that there is no perceived OOPness about the dot operator. When we consider what the dot operator does, it would have been just as acceptable to not have an operator. It's inclusion was merely a syntax decision.

    For example, any number of std::string's functions would be close to the same. (Forget that this is the C board for a minute)

    std::string foo = "Life, the universe, and everything.";
    std::string::size_type len = length (foo);
    std::string::size_type pos = find (foo , "the");

    Only that you have to pass a reference to the accessed object.

    The arrow operator is just a construct for member access from pointers; it's still the same example.

    len = length (*bar);
    pos = find (*bar, "the");

    We probably wouldn't have an extra operator if it were like this.

  12. #12
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    getter and setter are useful if you use them right.

    Code:
    const char * getName(struct foo * f)
    {
      if( f->name == NULL ) 
         return "foobar";              // our policy
      return f->name;
    
    }
    getters and setters

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    No one really argued that though ... somehow we lost track of what OP was really asking and I still think LordPc was defending the sweetness of some syntactic sugar.

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by LordPc View Post
    im talking about oo programming. im not sure you are...
    C is a OO language..... NOT!

    Did you catch the Borat joke there?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Overloading -> operator
    By Hunter2 in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2004, 03:08 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM