0

I currently have the following Circularly Doubly Linked List as the parent (provided by the professor of my class):

template <class datatype>
class CDLL
{
public:
    struct node;
    class iterator;

    // Constructors
    CDLL(void);
    CDLL(unsigned int n_elements, datatype datum);
    CDLL(const CDLL& rlist);
    CDLL(iterator begin, iterator end);

    // .. code ...

};

Our instructions were to create a Queue that inherits from this CDLL:

template <class datatype>
class Queue : protected CDLL<datatype> {
public:
    using CDLL<datatype>::CDLL;

    // ... code ...
};

In my tests I have:

Queue<int> x = Queue<int>(2, 1); // Creates a queue of: [1, 1]
Queue<int> * y = &Queue<int>(2, 1); // Creates a queue of: []

I have debugged this thoroughly and it walks through the constructor steps (pushing each element to the queue/cdll) and it walks through every step. When it pops out of the cdll constructor, it "forgets" everything it's done. The address of this in the cdll constructor matches the address of y. I've also tried Queue<datatype>::Queue() : Queue::CDLL<datatype>() {} but the same behavior persists.

I've taken a look at:

C++ Inheritance constructor override, What are the rules for calling the superclass constructor? and a few other questions with titles similar to this one but I can't seem to explain the behavior of this.

I have surfed google/SO and have tried many of the solutions many have suggested but to no avail.

Community
  • 1
  • 1
npengra317
  • 126
  • 2
  • 7

1 Answers1

1

This:

Queue<int> * y = &Queue<int>(2, 1);

Is not valid C++. It is apparently accepted by an MSVC extension. The issue is that, while this extension allows you to take the address of the temporary, it does not extend its lifetime; which means that this instance of Queue<int> is destructed as soon as the initialization of y is completed, leaving y dangling and its further inspection via the debugger nonsensical.

To disable MSVC extensions, use the /Za compiler switch.

Quentin
  • 62,093
  • 7
  • 131
  • 191
  • What if I were to create an array of `**cdll` where I want to place queues and `cdlls`? – npengra317 May 20 '17 at 23:37
  • @npengra317 `CDLL *p = &x;` works fine, via implicit pointer conversion. You will also probably get to dynamic memory allocation and smart pointers before dealing with collections of polymorphic objects (such as an array owning `Queue`s and `CDLL`s). – Quentin May 20 '17 at 23:40
  • Understood, I will cite this to my professor. Thank you for your help. – npengra317 May 20 '17 at 23:45