Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why is a virtual desconstructor called when a derived class object created?

33 views
Skip to first unread message

fl

unread,
Oct 4, 2015, 7:53:59 PM10/4/15
to
Hi,

When I trace through a derived class object, it is found that the base class
virtual de-constructor is called in the last step of the object creating code.

Here is the base class:
...........
class Item_base {
public:
virtual Item_base* clone() const
{ return new Item_base(*this); }
public:
Item_base(const std::string &book = "",
double sales_price = 0.0):
isbn(book), price(sales_price) { }

std::string book() const { return isbn; }

virtual double net_price(std::size_t n) const
{ return n * price; }

// no work, but virtual destructor needed
// if base pointer that points to a derived object is ever deleted
virtual ~Item_base() { }
private:
std::string isbn; // identifier for the item
protected:
double price; // normal, undiscounted price

};



Below is the derived class:

class Sales_item {
public:
// default constructor: unbound handle
Sales_item(): p(0), use(new std::size_t(1)) { }
// attaches a handle to a copy of the Item_base object
Sales_item(const Item_base&);

// copy control members to manage the use count and pointers
Sales_item(const Sales_item &i): p(i.p), use(i.use)
{ ++*use; }
~Sales_item() { decr_use(); }
Sales_item& operator=(const Sales_item&);

// member access operators
const Item_base *operator->() const { return p; }
const Item_base &operator*() const { return *p; }
private:
Item_base *p; // pointer to shared item
std::size_t *use; // pointer to shared use count

// called by both destructor and assignment operator to free pointers
void decr_use()
{ if (--*use == 0) {delete p; delete use;} }
};


The C++ code is:
int main()
{
Sales_item item1(Item_base("123", 45));

What I see is that before jumps to the next line, item1 calls

virtual ~Item_base() { }

in the base class. What is the reason for this call?

Thanks,

Ian Collins

unread,
Oct 4, 2015, 8:05:03 PM10/4/15
to
fl wrote:
> Hi,
>
> When I trace through a derived class object, it is found that the base class
> virtual de-constructor is called in the last step of the object creating code.

"destructor"

<snip>
>
> The C++ code is:
> int main()
> {
> Sales_item item1(Item_base("123", 45));

^^^^^^^^^
>
> What I see is that before jumps to the next line, item1 calls
>
> virtual ~Item_base() { }
>
> in the base class. What is the reason for this call?

You are creating a temporary Item_base object.

--
Ian Collins

Ian Collins

unread,
Oct 4, 2015, 8:08:56 PM10/4/15
to
fl wrote:
> Hi,
>
> When I trace through a derived class object, it is found that the base class
> virtual de-constructor is called in the last step of the object creating code.
>
> Here is the base class:
> ............
> class Item_base {
> public:
> virtual Item_base* clone() const
> { return new Item_base(*this); }
> public:
> Item_base(const std::string &book = "",
> double sales_price = 0.0):
> isbn(book), price(sales_price) { }
>
> std::string book() const { return isbn; }
>
> virtual double net_price(std::size_t n) const
> { return n * price; }
>
> // no work, but virtual destructor needed
> // if base pointer that points to a derived object is ever deleted
> virtual ~Item_base() { }
> private:
> std::string isbn; // identifier for the item
> protected:
> double price; // normal, undiscounted price
>
> };
>
>
>
> Below is the derived class:
>
> class Sales_item {

By the way, what makes you call this a derived class?

--
Ian Collins

fl

unread,
Oct 4, 2015, 10:02:25 PM10/4/15
to
Thanks Ian. Your two questions are the key to my problems. I just realized
that it is not a derived class. Thus, it is a temporary class variable. I
understand it now.
0 new messages