|
-
November 23rd, 2017, 12:52 AM
#1
Can I use the same variable to initialise two class objects?
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
Code:
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:
Code:
for(int i = 0; i<inputVal; i++){
temp = new Device;
myList.append(temp);
}
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!!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|