Click here to Skip to main content
15,668,167 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to create and initialise objects of a class called Device in my main program. But the number of objects created are based on the user input. The IDE is Qt so I have access to data structures like QLists.

What I have tried:

My idea was that in the header file I would declare a Device pointer like this
Device* temp; QList<Device*>myList;
then in the source file, I would initialise this in a for loop based on the number of inputs. Like so:
for(int i = 0; i<inputVal; i++){
     temp = new Device;

I can't run the code yet as it is part of a bigger module, so I wanted to know if this is completely wrong from a theoretical perspective. I was hoping that I could reuse the variable name temp and in the end, temp would contain the pointer to the last Device object created, and to clean up the memory I would use a foreach loop on the QList.

Please advice!!
Updated 22-Nov-17 21:17pm

1 solution

You can do that but it is bad style. A temporary variable should be local and not a class member:
for (int i = 0; i < inputVal; i++)
     Device *temp = new Device;
There is even no need to use a temporary variable:
for (int i = 0; i < inputVal; i++)
     myList.append(new Device());

To access the last item just call myList.takeLast() myList.last().

The cleanup function should be called from the destructor of the class that owns myList and might be called from elsewhere if required:
    // Other cleanup here


void MyClass::DeleteAllDevices()
    while (!myList.isEmpty())
        delete myList.takeFirst();
Share this answer
CPallini 23-Nov-17 4:14am    
TheLostJedi 23-Nov-17 6:56am    
This is exactly what I was looking for! And more! Question: If one of the Device objects gets destroyed, what will happen to the QList member? Will it become a null pointer? I have a unique ID for each of these objects, I was thinking of using a QMap<int,Device*> and a signal in the destructor of the Device class, so that I can delete the member if it gets destroyed during runtime.
Jochen Arndt 23-Nov-17 7:44am    
If you delete a Device you should remove that item also from the list because
the - then invalid - pointer will be still there.

Note what takeFirst() and takeLast() are doing: They will return the pointer but remove the first/last item from the list (therefore I had to correct my answer regarding accesing the last item).

delete takeAt(ndx);

to do the same for a Device by index or removeAt() to remove items from the list where the Device has been already deleted.

If you don't know the index of a Device that has been deleted already, iterate over the list items until you find the pointer and remove that item.

I suggest to manage the Devices at one place where the class containing the list would be the best place. Provide a function to delete a device by passing the pointer or the ID which then searches the list.

That avoids using a signal. If you really need a signal (can't call the delete function directly) I would choose a signal that triggers the delete.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900