This is what OO programming is all about. Use std::string
like comment above, or create a class that handles the data for you:
class MyObj {
private:
char *data;
int len;
public:
MyObj( const char *data, int len ) {
this->data = new char[...
// do what you need here
}
const char* getStr() const {
return data;
}
int getLen() const {
return len;
}
~MyObj() {
delete [] data;
}
};
You'll probably also want to implement copy constructor and assignment operator (or privatize their use)...
Alternatively, use std::string as your base implementation but only expose what and how you want:
class MyObj : private std::string {
public:
// whatever you want here
const char* data() {
return c_str();
}
int length() const {
return std::string::length();
}
};