|
|
|
|
const where needed): |
|
|
|
std::cout << alex does not output the characters from returned starting pointer directly. I bring into argument the following code which I have just tested and it's working: |
|
func() is a relative equivalent of the exact same procedures which are happening between friend operator<<() and String class from my initial posted code. Why on this case std::cout from main() outputs the characters correctly, while on original case it outputs nothing?std::cout from main() will handle the returned OStream object and print everything out, but since I bumped into this issue I want to learn from it before I move one. Thank you for baring with me.
|
|
|
|
|
|
|
|
|
|
std::cout << object; considering the fact that I have returned a pointer to string array? std::cout << foo << bar really means (std::cout << foo) << bar, which, if the overload returns the LHS operand, is equivalent to |
|
std::cout << foo << bar may not compile, or it may have different semantics to writing two separate statements (actually, this can sometimes be useful to achieve specific effects).std::cout << object << "my string", because you can't send output to a 'char *'. |
|
| Your second snippet should not compile. |
s.string twice. | Your first snippet should print the contents of the string |
| I don't know what your comments on lines 2 and 8 mean. |
This line prints xyz it means that if I remove that line from entire code, xyz will not be printed anymore, for that single line declaration. |
|
|
|
| It does. I've just checked it. I use DevC++. It outputs the content of s.string twice. |
|
|
| cout_test.cpp: In function 'std::ostream& operator<<(std::ostream&, const A&)': cout_test.cpp:9:12: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream<char>&}' from expression of type 'char [6]' |
s.string with o << s.string when I wrote the code in forum editor. But still, this does not help me understand the original issue: why first snippet prints content of s.string once while second snippet prints it twice? Or in other words: why on first snippet, line 8 does not print the characters from s.string even if I returned a char pointer to the first element of s.string? |
|
|
|
|
|