Let's start with the text member. Why have you declared it as pointer? If no other reasons speak against it, just declare it as
string text;
And thereby you don't have to care about its allocation and deletion.
The getText accessor is in principal fine. Here I would just consider two things:
- not all programs do string handling with the std::string class; it would be more general to just return a char* (or wchar_t*) to a null-terminated string. In that case you would be using the c_str function.
- why not make the access function
const
and declare thereby that it's not modifying the base object?
The setText setter function is ok. When you declart text as string (and not as a pointer) it will look a lot simpler:
void HelloWorld::setText (const string& t)
{text = t;}
Note that I made the argument a const reference, which avoids copying the string to a temp variable.
Just a warning: Do NOT return a const reference from a getter, except you know that the object you are returning has a longer live span than the function.