is const (c++) optional?

2020-02-07 02:48发布

according to some tutorials i read a while back, the "const" declaration makes a variable "constant" ie it cannot change later.
But i find this const declaration abit inconveniencing since the compiler sometimes gives errors like
"cannot convert const int to int"
or something like that.

and i find myself cheating by removing it anyway.

question: assuming that i am careful about not changing a variable in my source code, can i happily forget about this const stuff?

Thanks in advance

标签: c++ const
11条回答
在下西门庆
2楼-- · 2020-02-07 03:25

Its always optional. If its all your code sure you can forget it ( I wouldn't recommend it, because it protects you), but when you interact with others, you're essentially providing a contract for them that you won't change their object or calling a function does not change the state of your object. This can be invaluable when you are not familiar with other's code, or you don't have the source.

查看更多
Root(大扎)
3楼-- · 2020-02-07 03:29

Changing things that shouldn't be changed is one of the most common sources of error. It is therefore worthwhile specifying const because it prevents you from doing something wrong. Why would you want to give that up?

const double PI = 3.14159265358979;

PI=4; // generates a compiler error (good)

There are some problems with the c++ notation, because a constant can only be initialized, not assigned the first time, and sometimes, you don't have the value at initialization time.

class A {
private:
  const int num;
public:
  A(int x, int y) : num(0) { // oops, I don't yet know what num should be
    while ( ... ) {

    }
    num = ...;
  }
};

The way out of this one is to define a private function that computes the value of num but sometimes that means that instead of one clean block of code in the constructor, you are forced to split it into sections in awkward ways, just so you can initialize the variable.

class A {
private:
  const int num;
  int computeNum(int x, int y) { ... }
public:
  A(int x, int y) : num(f(x,y)) {
  }
};

Sometimes, you have a value that is generally supposed to be const, but you want to selectively override that when it semantically makes sense. For example, social security numbers don't change, unless your identity is stolen. So you have just one method, called createNewSSN() which changes the otherwise constant ssn

class Person {
private:
  const int ssn;
public:
  Person(int ssn_) : ssn(ssn_) {}

  void createNewSSN(int newssn) {
    log << "Changed SSN: " << ssn << " to " << newssn << "\n";
    *(int*)&ssn = newssn; // trust me, this is a special case....
  }
};
查看更多
放荡不羁爱自由
4楼-- · 2020-02-07 03:37

You can forget about it but isn't it nice if the compiler forces it upon you? That way, your "const" variables actually stay constant.

查看更多
Bombasti
5楼-- · 2020-02-07 03:37

People usually face this problem when they start using the const keyword. Believe me, it really helps. Leave it to the compiler to take care of the cosntness of the variable, instead of you taking care to not alter its value anywhere.

查看更多
放我归山
6楼-- · 2020-02-07 03:39

The idea behind using 'const' is specifically that you ensure compiler errors when attempting to alter a variable's value when it has been predetermined (by using const) that you do NOT want to do so. It's essentially built in error-checking, and is useful for many reasons.

This is especially valuable in cases such as an external interface and public methods as a way of guaranteeing the caller that a passed value will not be modified.

const also locks in the intent to not modify, and prevents accidental assignment.

While making const use mandatory is unnecessary, it is very useful and good practice.

Here's a useful explanation you may want to check out: http://duramecho.com/ComputerInformation/WhyHowCppConst.html

查看更多
仙女界的扛把子
7楼-- · 2020-02-07 03:40

Even if you and everyone you work with never makes mistakes, the use of const in your method declarations helps to document your interface.

查看更多
登录 后发表回答