huihut/interview

Is it a typo?

pstricks-fans opened this issue ยท 4 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.