Pointers to Classes || pointers to structures

This is a discussion on Pointers to Classes || pointers to structures within the C++ Programming forums, part of the General Programming Boards category; -> is not for pointers in classes. It's for pointers to classes. it can access any variable within a class...

  1. #16
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    -> is not for pointers in classes. It's for pointers to classes. it can access any variable within a class
    Woop?

  2. #17
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by C++Child
    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.
    You use -> when the left side is a pointer. In that example, pBrian is a pointer, so you must use "->" instead of "." to access loserPower.

  3. #18
    nbk
    nbk is offline
    meow nbk's Avatar
    Join Date
    Jul 2004
    Posts
    45
    The left is the pointer. Now, remember,

    Code:
    ptr->value = 0
    
    //is
    
    (*ptr).value = 0
    
    //It's also the equivalent to(in say, a small proggie usually)
    
    class.value = 0
    (which sums everything up )
    Last edited by nbk; 07-28-2004 at 11:07 PM.

  4. #19
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    Omg! I didn't know it was legal to make pointers point to instances. I get it now! the format is <pointer to instance>-><variable OR pointer>, right? Here:
    Code:
    #include<iostream.h>
    #include<string>
    #include<conio.h>
    struct hi
    {
    string so;
    };
    
    int main()
    {
    hi joe; //lol, it seems like I'm saying hi to a person named joe
    int * ok = &joe;
    ok->so = "Hi!";
    cout<<ok->so;
    cout.flush();
    getch();
    return 0;
    }
    Quote Originally Posted by nbk
    class.value = 0
    Is it legal to do the class name on the left side of '.' ? I thought you had to create an instance and it was instance.value = 0!
    Last edited by C++Child; 07-30-2004 at 02:34 PM.
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  5. #20
    nbk
    nbk is offline
    meow nbk's Avatar
    Join Date
    Jul 2004
    Posts
    45
    I meant an instance

    (wasn't explained right)
    joe.so = "sas"

    There ya go

  6. #21
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Let me try

    Code:
    object my_object;
    object *my_pointer;
    my_pointer = & my_object;
    my_object.value = 5;
    my_pointer->value = 5;
    // The last two lines essentially do the same thing, except the latter uses a pointer
    // Here, the advantages would be that of any pointer
    edit: nevermind - never saw there was a second page

  7. #22
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by C++Child
    Code:
    hi joe; //lol, it seems like I'm saying hi to a person named joe
    int * ok = &joe;
    ok->so = "Hi!";
    Close. The pointer has to be the same type as the instance (except for when using inheritance). So the code above should be:
    Code:
    hi joe; //lol, it seems like I'm saying hi to a person named joe
    hi * ok = &joe;
    ok->so = "Hi!";

  8. #23
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    Stop getting confused with the instance and the class name!
    Quote Originally Posted by jilou
    The pointer has to be the same type as the instance.
    .
    .
    hi * ok = &joe;
    hi Is the class name. Are you sure you meant the instance? If you meant the instance then you should have written this
    Code:
    joe * ok = &joe;
    EDIT: NVM, Did you mean the type of the instance? If so, then hi IS the type of instance.
    Last edited by C++Child; 07-30-2004 at 06:19 PM.
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  9. #24
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    jlou is correct.
    The pointer type must be a pointer of the instance type. That is, you cannot have a pointer to an int (int*) pointing to something that is not of type int (in this case, the type 'hi').

  10. #25
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>That is, you cannot have a pointer to an int (int*) pointing to something that is not of type int
    The exceptions being void* and unsigned char*. A pointer to any type can be stored in a void* as long as it isn't dereferenced, and any simple type, often called plain-old-data, can be referenced through an unsigned char*. This is called type punning. However, void*'s aren't used often in the C++ world and type punning is error prone at best. The rules are rather sticky.

Page 2 of 2 FirstFirst 12
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, 02: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

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