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?
4 ответов
+ 3
Oh....thx~
+ 1
so const* char represent a string type?🤔