New to C++. Question about constant pointers

前端 未结 10 1895
梦谈多话
梦谈多话 2021-02-10 06:55

I am trying to learn C++ via some web tutorials. I don\'t have a compiler available to me, otherwise I would try this out. I\'m not sure what is meant by a const pointer. Doe

相关标签:
10条回答
  • 2021-02-10 07:24

    Since you're new to C++, for the answer to this question and many other questions you may have or don't know you have, check out the C++ FAQ

    0 讨论(0)
  • 2021-02-10 07:31

    Const pointer could mean a few different things. I recommend checking out the C++ FAQ Lite on the matter.

    You can have:

    const int* p;
    int* const p;
    const int* const p;
    

    All three mean different things. Yes, it's kind of confusing.

    In your case, you have the second, which means you have a constant pointer to a non-constant object. That is, you can change the value of the integer via the pointer, but you can't change what the pointer points to. So the code you posted would not be legal.

    0 讨论(0)
  • 2021-02-10 07:32

    I don't have a compiler available to me, otherwise I would try this out

    Everyone has a C++ Compiler available to them: http://www.online-compiler.com

    There are many others, but this seems to work...

    0 讨论(0)
  • 2021-02-10 07:33

    You can't learn to drive a car just by reading books.

    Get yourself a C++ compiler if you want to learn C++. g++ is free of charge, as well as Visual Studio 2008 Express Edition.

    As for your question, a const pointer is a zone of memory that is ready only. Example: A class may provide read-only access to an internal buffer.

    Note that you also have the const pointer that is also const, aka

    const char * const p 
    

    In that case, even the value of the pointer cannot be modified.

    0 讨论(0)
提交回复
热议问题