dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Can I use the same variable to initialise two class objects?

  1. #1
    Join Date
    Apr 2017
    Posts
    18

    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!!

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,526

    Re: Can I use the same variable to initialise two class objects?

    And what are going to do if some Device object is about to destruct?
    Are you going to remove its pointer from the QList<Device*>? How?
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2017
    Posts
    18

    Re: Can I use the same variable to initialise two class objects?

    Quote Originally Posted by VictorN View Post
    And what are going to do if some Device object is about to destruct?
    Are you going to remove its pointer from the QList<Device*>? How?
    I honestly hadn't considered this. I had only thought about cleaning up. Each Device object has a unique ID, which the user provides. I could save the Device pointers along with their IDs in a hash table of sorts (QHash or QMap<int,Device*>). This could allow me to delete the specific pointer. And I could add an event in the destructor, so I am informed if a particular object is deleted.
    Does this sound right? Or am I overcomplicating what should be a straightforward task?

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,526

    Re: Can I use the same variable to initialise two class objects?

    Quote Originally Posted by VBCherry View Post
    I honestly hadn't considered this. I had only thought about cleaning up. Each Device object has a unique ID, which the user provides. I could save the Device pointers along with their IDs in a hash table of sorts (QHash or QMap<int,Device*>). This could allow me to delete the specific pointer. And I could add an event in the destructor, so I am informed if a particular object is deleted.
    Does this sound right? Or am I overcomplicating what should be a straightforward task?
    I am not sure you need this global pointer
    Code:
    Device* temp;
    at all.
    Victor Nijegorodov

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,877

    Re: Can I use the same variable to initialise two class objects?

    I have to create and initialise objects of a class called Device
    Unless you need to store object pointers, why not just have a list of class Device?

    Code:
    QList<Device> myList;
    or

    Code:
    QMap<int, Device> Mymap;
    if you want easy access to a particular Device.

    You then don't have to worry about memory management. If you want to remove an item just deleting the item in the container will work just fine.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.3.5)

  6. #6
    Join Date
    Apr 2017
    Posts
    18

    Re: Can I use the same variable to initialise two class objects?

    Quote Originally Posted by VictorN View Post
    I am not sure you need this global pointer
    I hadn't considered this. This would at least relieve my concern that I am reusing the same variable.

  7. #7
    Join Date
    Apr 2017
    Posts
    18

    Re: Can I use the same variable to initialise two class objects?

    Quote Originally Posted by 2kaud View Post
    Unless you need to store object pointers, why not just have a list of class Device?
    If I use pointers I then don't have to worry about the object going out of scope somewhere and getting deleted. I know that until I call delete, the pointer stays put. I could be totally wrong on this though.

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,877

    Re: Can I use the same variable to initialise two class objects?

    Have you considered using smart pointers?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.3.5)

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)