简体   繁体   中英

Const class object with a pointer member

let's have an example program:

struct Example
{
    int* pointer;
    Example(int* p) : pointer(p) {}
};

int main()
{
    int var = 30;
    const Example object(&var);
    object.pointer = &var; // error
    *object.pointer = var;
}

I do know why the error appears - when creating a const Example object, pointer is actually const pointer, not a pointer to const. Therefore, when assigning &var to object.pointer it is not correct but assigning var to the *object.pointer works perfectly fine. So is there any way to make this line:

*object.pointer = var;

not compile? I want the value that pointer points to to also be const. In other words, when creating a const object:

const Example object(&var);

I want constness to be applied also on the value itself, not only on the pointer:

const int* const pointer; // something like that

Either declare the data member like

const int *pointer;

or make it a protected or private data member. In the last case you can declare a non-constant member function that allows to change the value pointed to by the pointer. For a constant object of the class the function will not be called. For example

struct Example
{
private:
    int* pointer;
public:
    Example(int* p) : pointer(p) {}
    int & get_value() { return *pointer; }
    const int & get_value() const { return *pointer; }
};
const Example object(&var);

This creates a const object. Once a const object is created it cannot be changed . This is what a constant object means, by definition.

object.pointer = &var;

This attempts to change this object's member. This object cannot be changed. It is a constant object now, hence the error.

I want constness to be applied also on the value itself, not only on the pointer:

 const int* const pointer; // something like that

"Something like that" declares a pointer that's both a constant value itself, and it points to a constant value.

If you want an object with a pointer to const int , that would be:

const int *pointer;

And making the object itself constant has absolutely nothing to do with it, whatsoever. It only prevents modification of the object itself. Once you make something const it cannot be changed for any reason .

You can do something like that by

  • Making pointer an member function that return the pointer
  • Having non-const version of the function return a reference to the pointer to allow assignment to that
  • Having const version of the function return a copy of the pointer to prevent assignment
struct Example
{
private:
    int* pointer_internal;
public:
    Example(int* p) : pointer_internal(p) {}
    int*& pointer() { return pointer_internal; }
    int* pointer() const { return pointer_internal; }
};

int main()
{
    int var = 30;
    const Example object(&var);
    object.pointer() = &var; // error: lvalue required as left operand of assignment
    *object.pointer() = var;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM