C++, Classes, Const, and strange syntax

后端 未结 8 2111
广开言路
广开言路 2020-12-18 02:14

I was re-reading c++ primer(4th ed.) today - the section on member functions and const references etc, and I came up with this wierd little program:

using st         


        
8条回答
  •  清歌不尽
    2020-12-18 02:51

    The constructor is allowed to modify the value of a const object, yes. But if it weren't, what could it do?

    Since the constructor has such access, it can "forward" it to someone else or "save" it for later. Of course, doing so might be a bad idea.

    This is one instance where the safety mechanisms of C++ do not prevent you from building an ill-formed program. C++ is anything but foolproof. So, just be careful!

提交回复
热议问题