New to C++. Question about constant pointers

前端 未结 10 1939
梦谈多话
梦谈多话 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:12

    G'day,

    To remember this easily you can use the trick that Scott Meyers describes in his excellent book "Effective C++" (sanitised Amazon link)

    You draw a line through the declaration where the asterisk is located.

    • If the keyword const appears to the left of the line, then you can't change the value of the item that you're pointing to.
    • If the keyword const appears to the right of the line, then you can't change the pointer to point to another location.
    • If const appears on both sides, then you can't change the pointer and you can't change the value of what you're pointing to.

    BTW That book is excellent, and while not for a beginner, is definitely a way of taking your C++ knowledge to the next level! Highly recommended.

    HTH

    cheers,

提交回复
热议问题