Does const on function parameters affect the signature and behavior in C++?
Hello everyone! I started learning C++ and encountered a confusing situation. When I write functions, I often use const for parameters to ensure they are not modified inside the function. For example:
```cpp
void print(const std::string& str) {
std::cout << str << std::endl;
}
```
But recently I noticed that if I declare a function without const but with the same name and types, the compiler does not consider it an overload:
```cpp
void print(const std::string& str) { /* ... */ }
void print(std::string& str) { /* ... */ } // This is an overload
void print(std::string str) { /* ... */ } // And this is an error or not?
```
Why is that? After all, const in a parameter is part of the type. But I heard that for parameters passed by value, const does not affect the signature. Is that true? And in general, is there any
No answers yet.
Log in to leave an answer