Thread: Q: Pointers to Classes '->' notation

  1. #1
    Compile Errors = Schwa?!
    Join Date
    Aug 2005
    Location
    Ohio
    Posts
    15

    Question Q: Pointers to Classes '->' notation

    I've recently picked C++ up again to attempt to learn it on my own as I don't have the time presently to go to a proper class. This time around my comprehension has been much better, however, I'm a bit confused with the '->' notation in relation to "Pointers to Classes."

    I'm currently reading Chapter 10 - Data Structures and Classes of the book "Introduction to Game Programming with C++" by Alan Thorn. As I run into topics I don't understand from one book I switch to another and read the same topic to see if another perspective helps me understand it better. In the case of "Pointers to Classes" I'm still just not wrapping my head around it.

    Here is the example the book offers:

    Code:
    Frog frog;
    Frog *Pointer_To_Frog = &frog;
    
    frog->eat();

    When I look at this I see an instance of the class "Frog" is being created and being named "frog." On the next line a pointer of the datatype "Frog" class is created and assigned the memory location for the instance "frog." This brings me to the line I don't fully understand. The way I see it is the function or method 'eat' of the instance 'frog' is being called. So why isn't the syntax just 'frog.eat();'? Unless the purpose of the example is to show how to get the memory location of the function 'eat();' using the pointer, in which case shouldn't the syntax be something like '*Pointer_To_Frog->eat();'? Or even '*Pointer_To_Frog->frog.eat'?

    Anybody see how I'm misunderstanding this? Perhaps a clearer example would make more sense I know the whole Class "Frog" instance "frog" can easily be erroneously interchanged.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The syntax is frog.eat or Pointer_To_Frog->eat(). frog->eat() is incorrect.

  3. #3
    Compile Errors = Schwa?!
    Join Date
    Aug 2005
    Location
    Ohio
    Posts
    15
    so I'm not an idiot, the book is just wrong / it's a typo. Alright than, thanks tabstop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ confusion
    By Eirik in forum Windows Programming
    Replies: 14
    Last Post: 04-29-2009, 01:54 PM
  2. problem with classes and pointers
    By Akkernight in forum C++ Programming
    Replies: 18
    Last Post: 02-21-2009, 06:21 AM
  3. Class with pointers to other classes as member data.
    By Sclorch in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2009, 05:59 AM
  4. Pointers, Classes, and Errors o my!
    By Scottc1988 in forum C++ Programming
    Replies: 12
    Last Post: 03-13-2003, 10:14 PM
  5. Pointers to inherited classes
    By sean in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2001, 03:04 PM