简体   繁体   中英

How compiler make's sure that no data member state is changed in the const member function? (either in C++ or java)

How can a C++ or Java compiler make sure that none of the member variables state is changed in a const member function (mutable is exceptional).

Will the compiler do something like putting the code in a non-writable code segment or something like that?

For C++ const checking is done at compile time through the logic of the compiler. It will ensure that if a function is marked const then no changes will be made to the member variables. I don't think it has anything to do with the storage of the executable code.

For Java, I wasn't aware that there was the same const paradigm.

The compiler doesn't make sure. It can't, since there's no rule in the language that says that member variable state cannot change in a const member function. The only rule is that you cannot change the state through the this pointer (without casting away const ).

A C++ compiler will flag an error if you try to modify a member variable (directly or indirectly by calling a non-const function) in a const function (unless that variable is marked as mutable). You can get around this with a cast. The compiler will not (and should not) do anything more to enforce constness at run-time.

I can't speak to Java.

In C++: const qualifier of the function just makes every non-mutable member of the class and this pointer const-qualified within this function body. That's all.

ADD:

It means that if const-function explicitly or implicitly accesses this pointer as non-const pointer it will produce compilation error.

In C++ : The compiler detects obvious errors, that is callings of a non-const function on a member variable (or the this object). It includes calling operator=, etc. It will usually output "cannot convert from const ...T... to ...T...".

If you want to bypass that, you can use const_cast .

It should be noted though that the constness of a function does NOT mean that the state of the object won't change after a call to. For example, with that code :

class A
{
public:
  // stuff...
  //
  int* getPointer() const {return mpA;}
private:
  int* mpA;
}

you can have access to the value pointed to by mpA from the outside and modify it... The object mpA itself won't be modified (so the constness of getPointer is respected), but the constness of the value that mpA points to is not guaranteed. It would indeed be too much work to guarantee it.

In C++ the const qualifier is a qualifier on the type. Actually, a const-qualified named is (after the standard) an elaborated type .

When performing function overload resolution, the compilers will thus apply the regular mechanism that prevents you from calling void foo(int) with a std::string as argument for example.

They may have, though, better diagnostics for this specific error, to help the developer.

In C++, this is especially illustrated by the fact that const_cast is available to remove the const-ness of an object... There is only one subtlety:

Objects that are instantiated at file scope (globals, statics, ...) and declared const may be placed in read-only memory at the discretion of the compiler, in which case attempting a const_cast is Undefined Behavior .

And one more to addition the above points to make it simple. In const functions , compiler makes it to constant pointer. so const int a =10;, a= 20 obviously throws errors.

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