C++: What are the differences between using "const" or not? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
0

C++: What are the differences between using "const" or not?

I learnt from this app that "const" is to make constant numbers or strings or something else. But when I was doing some practice on operator overloading on my own, some errors occurred if I didn't use "const" with parameters in the operator overloading function. (Sorry for poor english) #include <iostream> #include <string> class sample { int data; std::string word; public: sample(){} sample(int a, const char b[]):data(++a),word(b){} sample operator+(sample& s) { sample d; d.word = this->word + s.word; return d; } }; int main() { sample me(1, "I am learning C++"); } In the above case, (In line 9 of the code) if I use "char b[]" instead of "const char b[]", some errors occurred. Why?

25th Nov 2019, 1:04 PM
汝風留名
汝風留名 - avatar
2 Respuestas
+ 3
Oh....thx~
25th Nov 2019, 2:00 PM
汝風留名
汝風留名 - avatar
+ 1
so const* char represent a string type?🤔
25th Nov 2019, 1:17 PM
汝風留名
汝風留名 - avatar