I have this code but compilation failed:
class TextBlock { public: TextBlock(std::string &s) { text = s; } private: std::string text; }; int main() { TextBlock tb("Hello"); std::cout<< tb[0] << std::endl ; } why when the constructor is TextBlock(const std::string &s) the above code can compile success ?
constthen the function is fine with taking a temporary because anyhow it wont be modified. Passing a temporary to a function that expects a non-const reference is pointless because you cannot observe the modifications, hence it makes sense that this is an error