Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I explicitly call an object's destructor in an instance of another class?
    primarykey
    data
    text
    <p>I'm implementing a stack using two queues as an exercise. I have two queue objects in every instance of a stack class. I would like the destructor of the stack to call the destructor of the queues. Looking online, it seems that explicit usage of destructors is not very common as they tend to be called automatically. My code:</p> <pre><code>template&lt;class T&gt; class Stack { // LIFO objects public: Stack(int MaxStackSize = 10); ~Stack(); bool IsEmpty() const {return addS.IsEmpty();} bool IsFull() const {return addS.getSize()==maxSize;} Stack&lt;T&gt;&amp; Add(const T&amp; x); Stack&lt;T&gt;&amp; Delete(T&amp; x); void Print() const; private: LinkedQueue&lt;T&gt; addS; LinkedQueue&lt;T&gt; delS; int maxSize; }; template&lt;class T&gt; Stack&lt;T&gt;::Stack(int MaxStackSize) { maxSize = MaxStackSize; } template&lt;class T&gt; Stack&lt;T&gt;::~Stack() { ~addS(); ~delS(); } template&lt;class T&gt; class LinkedQueue { // FIFO objects public: LinkedQueue() {front = rear = 0;} // constructor ~LinkedQueue(); // destructor bool IsEmpty() const {return ((front) ? false : true);} bool IsFull() const; T First() const; // return first element T Last() const; // return last element LinkedQueue&lt;T&gt;&amp; Add(const T&amp; x); LinkedQueue&lt;T&gt;&amp; Delete(T&amp; x); void Print() const; // print the queue in order int getSize() const; private: Node&lt;T&gt; *front; // pointer to first node Node&lt;T&gt; *rear; // pointer to last node }; template&lt;class T&gt; LinkedQueue&lt;T&gt;::~LinkedQueue() {// Queue destructor. Delete all nodes. Node&lt;T&gt; *next; while (front) { next = front-&gt;link; delete front; front = next; } } </code></pre> <p>Running the above code gives me the following error:</p> <blockquote> <p>stack.h: In destructor ‘Stack&lt; T >::~Stack() [with T = int]’: stackrunner.cc:9: instantiated from here stack.h:37: error: no match for call to ‘(LinkedQueue&lt; int >) ()’</p> </blockquote> <p>Am I calling the destructors incorrectly? Should I not be calling the destructors at all? Are object destrcutors called automatically when the class destructor is called?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload