Part of a task for a homework is to load two text-files and save their content in a class using dynamically allocated char-arrays.
This is my class. What can I improve about it?
Content.hpp
class Content
{
public:
Content(char* pContent);
~Content();
char* getContent();
private:
char* data;
};
Content.cpp
#include <cstring>
#include "Content.h"
using namespace std;
Content::Content(char* pContent){
data = new char[sizeof pContent];
strcpy(data, pContent);
}
Content::~Content(){
delete[] data;
}
char* Content::getContent(){
return data;
}
You should replace
sizeof pContentwithstrlen(pContent) + 1, if you are storing strings (which it appears you are). This is because character arrays will decay to pointers in C & C++, which hold no length.