Your orginal Example5::content() method (on line 21 or your opening post) wasn't returning anything, so it's hardly surpising the compiler didn't like line 21, where you try to display the void return value.
And inside Example5::content() you:
1. have a typo on line 22 (I assume you meant
this->ptr
rather than
this-ptr
2. are outputting the addresses of the pointers rather than the values they point to. (But I see that in the content1() and content2() methods of your newer version of the code you are dereferencing the pointers to obtains the values.)
But I would like to print everything with just one function "content" but when I declare a function returning an object of that class, everything doesnt match... |
What about?
1 2 3 4 5 6
|
string content() const { // not does not return a const ref as str is a local string
string str = *ptr;
str += " ";
str += *pmr;
return str;
}
|
(have you looked into string manipulation yet, inc concatenation?)
But the better solution would be to overload operator<< for your type.
and to be honest I dont know yet why I should use new-ing std::string.... |
Why you should? Or shouldn't??
std::string is pretty much just a pointer wrapped up in a class so you don't have to worry about new-ing and deleting a char buffer.
1 2 3 4 5 6 7
|
class string {
private:
char* data;
public:
string() : data(nullptr) {}
// etc
};
|
so by new-ing you are doing one more new, for the char buffer, than really needed. And making things more dangerous than required.
(real std::string implementations also keep track of the size, etc -- for optimization reasons -- but they should still be used directly and not via a pointer.)
Andy