std::queue initialization with NULL

12,668

Solution 1

If you write this:

std::queue<Test*> testQueue; //it is default initialized

then that is enough; no need to make it pointer and initialized it with NULL.

Also, you can do this:

if ( testQueue.empty()) 
{
    //testQueue is empty
}

Solution 2

If you use a pointer to a queue, you can certainly initialize it to a null pointer (but you probably want to avoid that, if possible).

If you simply define a queue, you can test whether it's empty (with, as you've suggested, .empty()).

Solution 3

To directly answer your question, you do not initialize it with a null pointer. By default, the queue is empty, and that's what you test (as suggested by the other answers). However, I think there is an underlying problem here that goes a little deeper than that, which is thinking too much in terms of pointers.

Since you are using a container, I would recommend against having a std::queue<Object *>. Instead, I would recommend to use a std::queue<Object> where possible (store the object, not the pointer). Rather than having any sort of null value to indicate that the object is not in a valid state, just initialize the value in the constructor (so all objects are in a valid state) and then all objects in your std::queue are valid. In C++, this should be your preference for how to use objects:

  1. Directly create objects on the stack or stored in a container (preferably an STL container) if you need to deal in collections of objects
  2. References (for instance, if you are passing an object into a function and want to avoid a copy, but here the reference refers to an object created via 1)
  3. A smart pointer (this should be the only time that new appears in your code). These should be used sparingly, and only when you have made certain that creating an object via 1 will not work.
  4. A regular pointer. This should almost never be used. In most of my projects, I do not use any raw pointers. In the rest, I only use them as a sort of caching mechanism, where a long calculation determines which object I'm operating on, and a null pointer means I haven't determined it yet. However, I still do not create the objects with their 'primary' handle being the pointer; the pointer points to an object I created via 1.

When determining what to store in your containers, you should also follow the above (prefer collections of objects first, then references, then smart pointers, then finally raw pointers).

Pointers may be common in Java, where most variables are created on the heap (or in C++ parlance, from the free store), and they may be somewhat common in C, where the STL, references, and smart pointers don't exist, but they do not have very much place in modern C++.

Solution 4

A queue is a C++ object with a constructor. You don't need to initialize it to a default value. It always begins as an empty queue. .empty() just checks whether it is empty or not. .clear() would remove its contents if needed, but on a newly constructed one, this is not necessary.

Share:
12,668
Aneesh Narayanan
Author by

Aneesh Narayanan

Updated on June 04, 2022

Comments

  • Aneesh Narayanan
    Aneesh Narayanan almost 2 years

    Is it possible to initialize a C++ std::queue with a NULL value like other variables?

    Like this:

    HANDLE variable = NULL; 
    
    class Test
    {
    }
    

    i.e.

    std::queue<Test*> testQueue = NULL;
    

    or

    testQueue.empty();
    

    or something like that?