Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

quick question C++ pointers

Options
  • 13-05-2003 2:53pm
    #1
    Moderators, Arts Moderators, Recreation & Hobbies Moderators, Sports Moderators Posts: 9,514 Mod ✭✭✭✭


    does the "entry*" in both lines below mean "something pointed to by entry"...or "a pointer to something of type entry"...


    int browse(entry *, int);
    void delete(entry *);


    trying to get something straight i mo ceann


Comments

  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    A pointer to entry.


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    On the same subject I suppose I should add:
    entry e;
    entry* a = &e;
    const entry* b = &e;
    entry const* c = &e;
    const entry const* d = &e;
    
    a, b, c and d all point to e. a and b can both be made to point to another entry, c and d cannot. a and c can be made to change e (call a non-const member function, which includes assignment) b and d cannot.
    entry f;
    a = &f; //allowed
    b = &f; //allowed
    c = &f; //won't compile
    d = &f; //won't compile
    
    *a = f; //allowed
    *b = f; //won't compile
    *c = f; //allowed
    *d = f; //won't compile
    
    Generally it's a good idea to use the most const version you can (i.e. if you don't change the object pointed to use a const pointer, not a non-const) as this will catch more mistakes at compile time, and it may allow for greater optimisation by the compiler.

    It's also worth noting that const references have a major advantages over non-const; that a cast can be used when passing to a const reference parameter. You can't cast when passing to a non-const reference parameter because any changes made to the object created by the cast can't be passed on to the object cast from (so banning it avoids some of the issues in in VB mentioned recently).


Advertisement