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:40

You lose some useful language features without const, especially regarding references. Consider:

void f(const MyClass& m);
void f(const int& i);

// ...

f(MyClass()); // OK, can pass temporary as const reference
f(5); // OK as well, 5 is a temporary int

If you consider the 'const' optional and get rid of it:

void f(MyClass& m);
void f(int& i);

// ...

f(MyClass()); // Error, cannot pass temporary as non-const reference
f(5); // Error, same as above
查看更多
对你真心纯属浪费
3楼-- · 2020-02-07 03:44

Are you serious? Why would you want to give up on such a useful feature just because you make a mistake sometimes? Better try and learn to avoid mistakes with const and you benefit from the great assistance it adds to ensure correctnes with your code.

Of course, you can say goodbye to all the help the language provides, and tell the compiler thereby not tell you about mistakes in your code anymore. Instead, you will have to ask the debugger later on where your bugs are. Not sure whether that's better.

查看更多
戒情不戒烟
4楼-- · 2020-02-07 03:45

To answer your question first:

Yes, you can. But only if you are careful, and everyone else who uses your code from now to eternity is also careful.

So, on balance you are better off thinking about why you should make something const and when you should not.

Another technique for exploring why const makes a difference is to try to make everything const at first until you have valid reasons to change something, then, and only then, remove the minimum number of consts until it works again.

Glad to see you are thinking about the issue - its more than most do.

查看更多
Anthone
5楼-- · 2020-02-07 03:48

In C++, "const" can a apply to a variable (making it unchangeable) or a function (rendering it unable to change other things).

My use of "const" is not just to prevent my code from changing my variable. It's to prevent some idiot's code from changing my variable (especially if the idiot is me six months from now) and to prevent my code from changing a critical variable some idiot left exposed (especially if the idiot was me six months ago).

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

If you are careful, yes. But it is human to err. Also, you do not give the compiler the opportunity to optimize around these constants.

The error message that you get is because you try to alter a const int. Simply assigning the value to a (non-const) int, you alter it as you want to.

Const helps you, try to sprinkle more of it around and listen to the compiler. That will help you produce better code.

查看更多
登录 后发表回答