Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it a typo? #78

Closed
pstricks-fans opened this issue Apr 7, 2021 · 4 comments
Closed

Is it a typo? #78

pstricks-fans opened this issue Apr 7, 2021 · 4 comments

Comments

@pstricks-fans
Copy link

pstricks-fans commented Apr 7, 2021

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?

@huihut
Copy link
Owner

huihut commented Apr 7, 2021

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

@kingFighter
Copy link

// 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

@pstricks-fans
Copy link
Author

  • 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.

@huihut
Copy link
Owner

huihut commented Apr 13, 2021

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

huihut added a commit that referenced this issue Apr 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants