huihut / interview

📚 C/C++ 技术面试基础知识总结,包括语言、程序库、数据结构、算法、系统、网络、链接装载库等知识及面试经验、招聘、内推等信息。This repository is a summary of the basic knowledge of recruiting job seekers and beginners in the direction of C/C++ technology, including language, program library, data structure, algorithm, system, network, link loading library, interview experience, recruitment, recommendation, etc.

Home Page:https://interview.huihut.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it a typo?

pstricks-fans opened this issue · comments

The following is taken from https://interview.huihut.com/#/en?id=const.

class A
{
private:
    const int a;                // constant object member, can only be assigned in the initialization list

public:
    // Constructor
    A() : a(0) { };
    A(int x) : a(x) { };        //  initialize list

   // others are removed for the sake of simplicity 

};

void function()
{
    // object
    A b;                        // ordinary object, can call all member functions, update constant member variables
     
   // others are removed for the sake of simplicity 
}

ordinary object, can call all member functions, update constant member variables

I think constant members cannot be updated. Is it a typo?

When the constant member variable is a pointer, it can be updated, as discussed in this issue.

// ordinary object, can call all member functions, update constant member variables
Normally constant member var would mean member itself is const(e.g. int * const), don't think we will call const int* as constant member - it is confusing

  • const int* or int const* is read as "a pointer to a constant integer"
    The object being pointed cannot be changed but the pointer can.

  • int* const is read as "a constant pointer to an integer"
    The object being pointed can be changed but the pointer cannot.

  • const int* const or int const* const is read as "a constant pointer to a constant integer"
    Both the object being pointed and the pointer cannot be changed.

@huihut : Could you review my post on StackOverflow here? I am trying to clarify const int* or int const* should not be regarded as constant members.

You are right, const int* or int const* should not be regarded as constant members, so constant members cannot be updated.