Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

quick question C++ pointers

  • 13-05-2003 02:53PM
    #1
    Moderators, Arts Moderators, Recreation & Hobbies Moderators, Sports Moderators Posts: 9,779 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