Thread: Constant pointers

  1. #1
    Registered User
    Join Date
    Nov 2006

    Constant pointers

    If I have this

    const HermitCrab* t = &b;
    how can I make sure it compiles. Also if I have this

                  HermitCrab a(1);
                  const HermitCrab b(3);
    Will Compile    Code…
    ____________  1  HermitCrab* const q = &a;
    ____________  2  const HermitCrab* p = &a;
    ____________ 3   HermitCrab* const m = &b;
    ____________ 4   const HermitCrab* r = &b;
    ____________  5  const HermitCrab* p = &b;
                    p = &a;
    are these answers right
    1) No because the pointer is read only
    2) Yes
    3) No because the object is constant therefore it most have a const object pointer
    4) Yes
    5) Yes

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >how can I make sure it compiles.
    Hmm, define HermitCrab with a GetShellSize member function and define b to be an object of type HermitCrab.

    >1) No because the pointer is read only
    And what does that have to do with calling a member function on the pointed to object?

    >2) Yes

    >4) Yes

    >5) Yes
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Erm, it takes what - 30 seconds to actually type that into a small program and see if you get any 'you can't do that it's const' type error messages.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  4. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 11:24 AM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM