0
class Foo
{
public:
    const int a;
    const int* x;
    int* const y;
    Foo() : a{ 0 }, y{ new int(20) } {
        x = new int(10);
    }
};

int main()
{
    Foo f;
    // f.a = 100; // cannot
    f.x = new int(100);
    // f.y = new int(100); // cannot

}

When const int a is defined as fields of a class, it is called a constant member. It must be initialized in the initializer list and cannot be changed afterwards.

How about const int* x (which is the same as int const* x) and int* const y? Which one should be called as a constant member? If "constant member" is defined as field that must be initialized in the initializer list and cannot be changed afterwards, then the constant member is y rather than x. Am I wrong here?

Edit

According to IntelliSense, y is a constant member.

enter image description here

OK. I am sure I am not wrong. I will delete this question shortly. Thank you for your participation!

Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165
  • 4
    @reece Please don't attempt to answer questions with comments. It prevents people from down voting incorrect information. – Brian61354270 Apr 12 '21 at 21:22
  • You're not wrong. `int* const y` declares a constant pointer – Emanuel P Apr 12 '21 at 21:23
  • 1
    I am actually trying to counter the following [post](https://github.com/huihut/interview/issues/78#issuecomment-814899414). – Second Person Shooter Apr 12 '21 at 21:25
  • Semi-related: [Handy tool](https://cdecl.org/) – user4581301 Apr 12 '21 at 21:33
  • I don't know if the term "constant member" is specified or if it's opinion based. – Thomas Sablik Apr 12 '21 at 21:35
  • `const` and "constant" mean two different things. For example, `const int const_but_not_constant = rand();` – Keith Thompson Apr 12 '21 at 21:35
  • Side note: What makes a class like this really annoying is the assignment operator effectively becomes impossible. You can make a custom assignment operator that does nothing with `const` members, but if you `a = b;` and `a` and `b` don't have the same values in the `const` members, weird smurf is almost guaranteed to happen sooner or later. – user4581301 Apr 12 '21 at 21:36
  • @KeithThompson https://en.cppreference.com/w/cpp/language/cv: "const - defines that the type is constant." – Thomas Sablik Apr 12 '21 at 21:38
  • 2
    @MoneySetsYouFree: The comment is wrong. A pointer to const data can be modified. A const pointer cannot be modified. – Mooing Duck Apr 12 '21 at 21:44
  • @MooingDuck: Yes. I agree with yours. – Second Person Shooter Apr 12 '21 at 21:45
  • @ThomasSablik That's not what the standard says, and IMHO that wording on cppreference.com should be corrected. `const` means read-only. "constant" refers to something that can be evaluated at compile time. – Keith Thompson Apr 13 '21 at 00:50

1 Answers1

3

The "const int* x" is a (non-const) pointer to a const int. Since x is non-const, it need not be initialized upon construction.

Here are some examples:

class C
{
public:
    C() :
      const_int(1),
      const_int_again(2),
      const_ptr_to_non_const_int(nullptr),
      const_ptr_to_const_int(nullptr)
    {}

private:
    const int const_int;
    int const const_int_again;
    
    const int* ptr_to_const_int; // Doesn't need initialized
    int const* ptr_to_const_int_again; // Doesn't need initialized

    int* const const_ptr_to_non_const_int;
    const int* const const_ptr_to_const_int;
    int const* const const_ptr_to_const_int_again;
};

You may find the cdecl.org website helpful.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
PFee
  • 249
  • 2
  • 10
  • Since it's a c++ question - would be good to add references too - or link to one of the other good answers (e.g. see this question https://stackoverflow.com/questions/2627166/difference-between-const-reference-and-normal-parameter) – Mr R Apr 12 '21 at 22:06
  • And a `T * const * const` vs a `T const * *` – Mr R Apr 12 '21 at 22:07