Some inheritance, ctor, dtor questions

Suppose I have two classes, as such.

class base;
class der : public base;

None are ADTs. Say I then construct an object...

base baseObject;

Is it possible to, at a later point, create an object of der, without constructing a new object of class base?

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?
jaded7 wrote:
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(new char[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.
Last edited on
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 .

Once again, this is purely out of interest.
Last edited on
Is it possible to, at a later point, create an object of der, without constructing a new object of class base?

No.

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?

No.
> Is it possible to, at a later point, create an object of der, without constructing a new object of class base?

No. Every object of type der will contain an anonymous object of type base.


> 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?

No.

> the destructor of type der has been called on this object, however the destructor for type base has not been called.

The destructor of der, after it has finished executing will invoke the destructor of base ;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

struct base
{
    base() { std::cout << "base::base()\n" ; }
    virtual ~base() { std::cout << "base::~base()\n" ; }
};

struct derived : base
{
    derived() { std::cout << "derived::derived()\n" ; }
    ~derived() { std::cout << "derived::~derived()\n" ; }
};

int main()
{
    base* b = new derived ;

    std::cout << "----------------\n" ;

    delete b ; // destructor of derived (because destructor of base is virtual)
}
output:
base::base()
derived::derived()
----------------
derived::~derived()
base::~base()
Topic archived. No new replies allowed.