Functions and Interfaces Const in C ++

As an example, I will use the following (trivial) interface:

struct IObject
{
  virtual ~IObject() {}

  virtual std::string GetName() const = 0;
  virtual void ChangeState() = 0;
};

Logic dictates what GetNameshould be a member function const, and ChangeStatenot should.

All the code I've seen so far does not follow this logic. That is, GetNamethe above example will not be marked as a member function const.

Is this laziness or negligence or is there a legitimate reason for this? What are the main flaws of me that force my clients to implement member functions constwhen they are logically called?


EDIT: Thanks for your answers. I think this is pretty much unanimous: laziness / ignorance is the reason that I see.

+3
2

, /. GetName() , IObject .

- GetName() (!) , mutable.

+9

?

. - , , .

, - const, ?

. ( , , . , , .)


, , , , , const, , const - . , const , .
const - , , const, . - ... , , const .
, , - , const. , , , , .

+5

Source: https://habr.com/ru/post/1746229/


All Articles