I have a abstract base class that hold data, and I need allocate memory to these data, and the other problem is that derivate class has the = operator overloaded and copy constructor, I would like to know how Can I assure that in the derivate class copy the members data from abstract base class will be copied too, follows the code exemple:
class A {
public:
A(const char* v) {
value = new char[strlen(v)+1];
strncpy(value, v, strlen(v));
}
A(const A &a) {
value = new char[strlen(a.value)+1];
strncpy(value, a.value, strlen(a.value));
}
virtual ~A() {
delete[] value;
}
A& operator=(const A& a) {
value = new char[strlen(a.value)+1];
strncpy(value, a.value, strlen(a.value));
return *this;
}
const char* get() const {
return value;
}
virtual void do_some() = 0;
private:
char *value;
};
class B: public A {
public:
B(const char *v, const char *n) : A(v) {
name = new char[strlen(n)+1];
strncpy(name, n, strlen(n));
}
B(const B &b) : A(b) {
name = new char[strlen(b.name)+1];
strncpy(name, b.name, strlen(b.name));
}
~B() {
delete[] name;
}
B& operator=(const B& b) {
A::operator=(b);
name = new char[strlen(b.name)+1];
strncpy(name, b.name, strlen(b.name));
return *this;
}
const char *get() const {
return name;
}
void do_some() {
std::cout << name << std::endl;
}
private:
char *name;
};
My doubts is, in some C++'s books says that is not a good idea to overload the operator = and declare a copy constructor in abstract base class, so how can I declare a copy constructor and operator= in derivate class and assure that the base class was copied properly?
In my project if I don't use data members at the abstract base class, I have to declare some data members in all derivate class, of course that is more than one, so I designed the abstract class with data member, but I don't know if is the better way to do.