Is it possible to, at a later point, create an object of der, without constructing a new object of class base?
If you cannot adapt base and its component, no, but that's hard to do for a beginner. In every component of base, and base itself you should:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
struct forcenoconstruct {};
struct VariableInBase {
VariableInBase() : Data(newchar[2]) {}
VariableInBase(const forcenoconstruct&) {}
char * Data;
};
class Base {
VariableInBase v;
VariableInBase vib2;
public:
Base() { /* Common constructor */ }
Base(const forcenoconstruct& c) : v(c), vib2(c) { /* No-constructor */ }
};
Base b; // VariableInBase initialized
Base unb(forcenoconstruct()); // VariableInBase not initialized
It's not going to be easy.
jaded7 wrote:
Similarly, if you have an object of class der, is there a way to destruct the der components, leaving you with a fresh object of type base?
Not really useful, but that's easy to do.
1 2 3 4 5 6 7 8 9
// in 'base':
virtual ~base() { /* Virtual Destructor, ensure right destructor calling */ }
base * pBase;
der * pDer = new der;
pBase = pDer;
// delete pDer <--- If you want to use pBase, DO NOT do this.
// You can, at any moment, destroy pBase or pDer receiving the same effect,
// thanks to the virtual destructor.
// pBase can be used as a base, as long as der derives from base.
This doesnt destruct 'der' variables, but pBase can be used.
Wasn't talking about polymorphism. I understand polymorphism, but, say you have a case like this:
1 2 3
// forward decs
class base;
class der : public base
1 2 3 4 5 6
// Within any function.
der* derPointer = new der;
// Both base and derived constructors have been called.
der::~der();
// This may not be the correct syntax
the intent is that the pointer derPointer is now pointing to an object of type base. This means the program has...
1. Constructed an object of base
2. Constructed the derived object
This means that the object is a valid object of type der
3. Destructed the derived object
The idea of #3 is not that we interpret it as a polymorphic pointer, but that the destructor of type der has been called on this object, however the destructor for type base has not been called.
Edit: To elaborate on point 3; essentially, I would like all dog-specific members freed, and to treat it as an object of type base .