Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to track "nested objects "?

How to track "nested objects "?

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingtutorialquestionannouncement
15 Posts 4 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    ADDENDUM I am still trying to learn how to pass parameters to object. My current "duplicating constructor" is because I am doing it wrong. My main object constructor is defined this way:

     MainWindow\_Bluetooth::MainWindow\_Bluetooth(QWidget \*parent) :
            QMainWindow(parent),
            m\_ui(new Ui::MainWindow\_Bluetooth),
            m\_status(new QLabel),
            m\_console(new Console),
            // insert MDI area here ??
            m\_mdiarea(new QMdiArea),
    
            m\_settings(new SettingsDialog),
            //! \[1\]
            //! add rfcomm ??
            m\_serial(new QSerialPort(this))
           // sequence 1st add HERE
           // must bbe passed to SettingsDialog
            // this is wrong parameter here ??
            //m\_TAB\_Connect(new A\_BT\_TAB\_DIALOG),
            //m\_TAB\_Ext(new A\_BT\_TAB\_DIALOG\_EXT)
    
    
          //! \[1\]
        { // function apply \\
    

    hence I am passing settings(new SettingsDialog), as a parameter. SettingsDialog constructor:

    SettingsDialog::SettingsDialog(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::SettingsDialog),
    m_status(new QLabel),
    // 3 sequence add dialogs
    // HERE wrong object
    m_TAB_Connect(new A_BT_TAB_DIALOG),
    // wrong object
    m_TAB_Ext(new A_BT_TAB_DIALOG_EXT),
    m_intValidator(new QIntValidator(0, 4000000, this))
    {

    its in question / problem passed parameter is m_TAB_Connect(new A_BT_TAB_DIALOG), now for the source of this problem I have TWO instances of SettingsDialog and they point to TWO instances of A_BT_TAB_DIALOG hence I end up with WRONG instances of A_BT_TAB_DIALOG Can I modify my code to have TWO instances of A_BT_TAB_DIALOG and make sure I point to the CORRECT instance ? In my not so technical terminology can I pass a parameter to have something like m_TAB_Connect(new A_BT_TAB_DIALOG), m_TAB_Connect_Ext(new A_BT_TAB_DIALOG), I am looking for a solution and I am open tor suggestions leading to proper C++ solution. Thanks

    M Offline
    M Offline
    Mircea Neacsu
    wrote on last edited by
    #6

    Salvatore Terress wrote:

    Can I modify my code to have TWO instances of A_BT_TAB_DIALOG and make sure I point to the CORRECT instance ?

    What do you mean by "correct"? In your code:

    SettingsDialog::SettingsDialog(QWidget *parent) :
    //
    // ... stuff
    //
    m_TAB_Connect(new A_BT_TAB_DIALOG), // creates a A_BT_TAB_DIALOG
    m_TAB_Ext(new A_BT_TAB_DIALOG_EXT), // creates a A_BT_TAB_DIALOG_EXT
    // ...
    {

    If you have somewhere:

    SettingsDialog obj1(parent);
    SettingsDialog obj2(parent);

    each one of them will have their own TAB_Connect and TAB_Ext. In other words obj1.m_TAB_Connect != obj2.m_TAB_Connect. Do you want both of them to point to the same TAB object?

    Mircea

    L 1 Reply Last reply
    0
    • L Lost User

      UPDATE / closed - no solution found. This was pretty futile task, there are few "private" objects I did not see until each "parent" level was expanded. This is an offshoot of my other post AND if it is NOT OK for me to continue posting - there are others who are commenting negatively about my too many posts" PLEASE DO NOT REPLY I do not want to waste "volunteers time ". I am currently working on nested objects main object ( patent _) secondary object ( child ) next object ( child ) I am trying to pass correct parameters between them I am debugging the flow using constructors and it looks as the compiler / linker access / flow in sort of backwards First trace is "next object " constructor .... and then I get lost tracing....UP next object is that normal ? That is all I wanted to ask. Thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #7

      This is a reply to your question in the Lounge, which as you well know is not the place for programming questions.

      Salvatore Terress wrote:

      where to find decent description of the this syntax m_status(new QLabel),

      It is part of the constructor for a MainWindow_Bluetooth object. And since that line follows the set of initializers, it means create a new QLabel object, and store its pointer in the variable m_status. Why you are doing that, only you can answer. Bottom line, study the documentation for C++ classes, where all the syntax is explained.

      1 Reply Last reply
      0
      • M Mircea Neacsu

        Salvatore Terress wrote:

        Can I modify my code to have TWO instances of A_BT_TAB_DIALOG and make sure I point to the CORRECT instance ?

        What do you mean by "correct"? In your code:

        SettingsDialog::SettingsDialog(QWidget *parent) :
        //
        // ... stuff
        //
        m_TAB_Connect(new A_BT_TAB_DIALOG), // creates a A_BT_TAB_DIALOG
        m_TAB_Ext(new A_BT_TAB_DIALOG_EXT), // creates a A_BT_TAB_DIALOG_EXT
        // ...
        {

        If you have somewhere:

        SettingsDialog obj1(parent);
        SettingsDialog obj2(parent);

        each one of them will have their own TAB_Connect and TAB_Ext. In other words obj1.m_TAB_Connect != obj2.m_TAB_Connect. Do you want both of them to point to the same TAB object?

        Mircea

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #8

        I am sorry - I should have deleted the _Ext . Currently I am asking about the variable( new object ) syntax elsewhere. I have a hard time finding /searching for it I just need a verbal expiation of THIS specific form / syntax or real reference to it.

        M K 2 Replies Last reply
        0
        • L Lost User

          I am sorry - I should have deleted the _Ext . Currently I am asking about the variable( new object ) syntax elsewhere. I have a hard time finding /searching for it I just need a verbal expiation of THIS specific form / syntax or real reference to it.

          M Offline
          M Offline
          Mircea Neacsu
          wrote on last edited by
          #9

          If you have an object like this:

          class Thing1 {
          public:
          Thing1 ();
          }

          You can use it in another object:

          class Composite {
          public:
          Composite ();
          private:
          Thing1* part;
          };

          Composite::Composite()
          : part (new Thing1)
          {
          }

          In Composite, the member part is a pointer to a Thing1. When a composite is constructed, first the program allocates on heap a new object of type Thing1 and invokes it constructor. Because constructor for Thing1 does not have any parameters there is no need for a list of parameters. Next step: You have another type of object Thing2 declared like:

          class Thing2 {
          public:
          Thing2 (int param=42);
          };

          and the Composite object has 2 members:

          class Composite {
          public:
          Composite ();
          private:
          Thing1* part;
          Thing2* other_part;
          };

          The constructor of Composite could be:

          Composite::Composite ()
          : part (new Thing1) //as before
          , other_part (new Thing2) //same as saying other_part(new Thing2(42))
          {}

          because now the constructor for Thing1 needs a parameter but the parameter has a default value and the compiler will call the constructor with said value. Obviously, if you need a different value for the parameter, you will have to add it:

          Composite::Composite ()
          : part (new Thing1) //as before
          , other_part (new Thing2(24) //explicit constructor parameter
          {}

          Going back to the original question (which you seem to have deleted) What you want to have is it something like:

          class Composite{
          public:
          Composite ();
          private:
          Thing1 *part;
          Thing1 *other_part;
          {}

          ? Try to rephrase your question it terms of simple objects to make it easier for us to understand.

          Mircea

          1 Reply Last reply
          0
          • L Lost User

            I am sorry - I should have deleted the _Ext . Currently I am asking about the variable( new object ) syntax elsewhere. I have a hard time finding /searching for it I just need a verbal expiation of THIS specific form / syntax or real reference to it.

            K Offline
            K Offline
            k5054
            wrote on last edited by
            #10

            Lets break this down: 1) The expression new Object creates a new instance of Object on the heap, and provides a pointer to the new object. 2) The expression foo( _something_ ) makes a function call. We might be able to deduce that m_status() is a member function to some object, both from your descriptions elsewhere and the use of the m_ decoration, but that's not necessarily the case. It could be you are using m_ as a decoration for a menu object or a motorhome object or some other mystery object.... Together we have f(new Object) which (1) constructs a new Object on the heap, and then (2) calls a function f with a pointer to the newly constructed Object. What we don't know is if Object has a default constructor. It might not, in which case, as you observe over in the lounge, you must use f(new Object(param, ...). There may be multiple constructors, so you'll need to check the documentation for the Object to see which one is most appropriate. If you create an object with new, at some point delete Object should to be used to recover the allocated memory1. It's seems unlikely the the called function would do that, as it cannot know whether the pointer to Object is a pointer to a stack or heap object. Trying to delete a stack object is almost certainly going to cause Bad Things™ to happen. Maybe an immediate abort, maybe an exception gets thrown or maybe just a silent mess up of your data, with no indication that bad things have happened until some time later in the program. So you're probably better off to not use new Object as a parameter to a function. In general you should prefer a smart pointer ( [Dynamic memory management - cppreference.com](https://en.cppreference.com/w/cpp/memory) ) to new/delete in new development. But maybe the function does expect its parameter to be newly constructed within the parameter list and will delete before it returns? We don't know. Footnotes: 1 This isn't strictly true. If you only create one instance of an object, the memory will be recovered when the program exits. A number of GNU C stdlib routines do this, allocating a block of memory when first called and re-using it as needed. The allocated memory is only "lost" to the executing program - assuming virtual memory and one o

            L 2 Replies Last reply
            0
            • K k5054

              Lets break this down: 1) The expression new Object creates a new instance of Object on the heap, and provides a pointer to the new object. 2) The expression foo( _something_ ) makes a function call. We might be able to deduce that m_status() is a member function to some object, both from your descriptions elsewhere and the use of the m_ decoration, but that's not necessarily the case. It could be you are using m_ as a decoration for a menu object or a motorhome object or some other mystery object.... Together we have f(new Object) which (1) constructs a new Object on the heap, and then (2) calls a function f with a pointer to the newly constructed Object. What we don't know is if Object has a default constructor. It might not, in which case, as you observe over in the lounge, you must use f(new Object(param, ...). There may be multiple constructors, so you'll need to check the documentation for the Object to see which one is most appropriate. If you create an object with new, at some point delete Object should to be used to recover the allocated memory1. It's seems unlikely the the called function would do that, as it cannot know whether the pointer to Object is a pointer to a stack or heap object. Trying to delete a stack object is almost certainly going to cause Bad Things™ to happen. Maybe an immediate abort, maybe an exception gets thrown or maybe just a silent mess up of your data, with no indication that bad things have happened until some time later in the program. So you're probably better off to not use new Object as a parameter to a function. In general you should prefer a smart pointer ( [Dynamic memory management - cppreference.com](https://en.cppreference.com/w/cpp/memory) ) to new/delete in new development. But maybe the function does expect its parameter to be newly constructed within the parameter list and will delete before it returns? We don't know. Footnotes: 1 This isn't strictly true. If you only create one instance of an object, the memory will be recovered when the program exits. A number of GNU C stdlib routines do this, allocating a block of memory when first called and re-using it as needed. The allocated memory is only "lost" to the executing program - assuming virtual memory and one o

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #11

              Thank you, that is exactly what I was asking about. Plain explanation,,,, I need to digest the "pointer to the object ". My program just broke big time , so I am busy trying find where... I have backup , but not up - to date... Thanks again... PS I need to stay away from lounge, those folks are in their own world ,,, definitely not same as mine

              K 1 Reply Last reply
              0
              • L Lost User

                Thank you, that is exactly what I was asking about. Plain explanation,,,, I need to digest the "pointer to the object ". My program just broke big time , so I am busy trying find where... I have backup , but not up - to date... Thanks again... PS I need to stay away from lounge, those folks are in their own world ,,, definitely not same as mine

                K Offline
                K Offline
                k5054
                wrote on last edited by
                #12

                Salvatore Terress wrote:

                I need to digest the "pointer to the object ".

                That's worrisome. That's basic C/C++ stuff. Something any programmer with more than a few weeks experience should at least have some familiarity with. You should also understand the relationship between a pointer and a reference.

                Salvatore Terress wrote:

                My program just broke big time , so I am busy trying find where... I have backup , but not up - to date..

                Get yourself hooked up with a version control system like Git or SVN. There are public servers for both. If you need to keep you code proprietary (or just don't wish to share), either can be set up locally. If you need a local SVN/Git server, that might be a great project for a PI. If you're using QDevelop, then a quick google suggests that it supports several Version Control systems. Even if you're the only developer, being able to review and/or revert can save you a whole bunch of time. And red faces.

                "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

                1 Reply Last reply
                0
                • M Maximilien

                  If I understand your question correctly. Constructors will (try to) construct derived objects before itself ie. the base object needs to be constructed to be able to construct the derived classes. class Base {}; class Child : public Base{} Base will be constructed before Child.

                  CI/CD = Continuous Impediment/Continuous Despair

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #13

                  Correct - that is why I used term "nested objects ". My goal is to have an application with logical , intuitive GUI . hence I have sort of main parent GUI object with children. I am using " cut and paste " example which is "pop=up" dialog based , and I have added another windowing part to it. Unfortunately I did not keep better track of these , basically TWO parallel methods. And that what caused the problem. With so much help received I am sure I can adjust my code to avoid further disasters.. and I do appreciate the help, very much.

                  1 Reply Last reply
                  0
                  • K k5054

                    Lets break this down: 1) The expression new Object creates a new instance of Object on the heap, and provides a pointer to the new object. 2) The expression foo( _something_ ) makes a function call. We might be able to deduce that m_status() is a member function to some object, both from your descriptions elsewhere and the use of the m_ decoration, but that's not necessarily the case. It could be you are using m_ as a decoration for a menu object or a motorhome object or some other mystery object.... Together we have f(new Object) which (1) constructs a new Object on the heap, and then (2) calls a function f with a pointer to the newly constructed Object. What we don't know is if Object has a default constructor. It might not, in which case, as you observe over in the lounge, you must use f(new Object(param, ...). There may be multiple constructors, so you'll need to check the documentation for the Object to see which one is most appropriate. If you create an object with new, at some point delete Object should to be used to recover the allocated memory1. It's seems unlikely the the called function would do that, as it cannot know whether the pointer to Object is a pointer to a stack or heap object. Trying to delete a stack object is almost certainly going to cause Bad Things™ to happen. Maybe an immediate abort, maybe an exception gets thrown or maybe just a silent mess up of your data, with no indication that bad things have happened until some time later in the program. So you're probably better off to not use new Object as a parameter to a function. In general you should prefer a smart pointer ( [Dynamic memory management - cppreference.com](https://en.cppreference.com/w/cpp/memory) ) to new/delete in new development. But maybe the function does expect its parameter to be newly constructed within the parameter list and will delete before it returns? We don't know. Footnotes: 1 This isn't strictly true. If you only create one instance of an object, the memory will be recovered when the program exits. A number of GNU C stdlib routines do this, allocating a block of memory when first called and re-using it as needed. The allocated memory is only "lost" to the executing program - assuming virtual memory and one o

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #14

                    Sorry, bu I need more explanations /help . Here is my class constructor

                    SettingsDialog::SettingsDialog(QWidget *parent) :
                    QDialog(parent),
                    m_ui(new Ui::SettingsDialog),
                    m_status(new QLabel),

                    m\_TAB\_Connect(new A\_BT\_TAB\_DIALOG),
                    
                    //m\_TAB\_Ext(new A\_BT\_TAB\_DIALOG\_EXT),
                    m\_intValidator(new QIntValidator(0, 4000000, this))
                    

                    {

                    here is a snippet of class definition A_BT_TAB_DIALOG *m_TAB_Connect = nullptr; you said that new A_BT_TAB_DIALOG), builds a pointer - to object A_BT_TAB_DIALOG and m_TAB_Connect(new A_BT_TAB_DIALOG), is a function call of function m_TAB_Connect that is not what I thought is happening and I hope it is not some kind of Qt "stuff" I am reading m_TAB_Connect(new A_BT_TAB_DIALOG), as passing pointer m_TAB_Connect of (new) object A_BT_TAB_DIALOG am I wrong? I am not trying to nitpick, but I am having an issue accessing correct instance of the parent class AND NEED to get this terminology correct.

                    L 1 Reply Last reply
                    0
                    • L Lost User

                      Sorry, bu I need more explanations /help . Here is my class constructor

                      SettingsDialog::SettingsDialog(QWidget *parent) :
                      QDialog(parent),
                      m_ui(new Ui::SettingsDialog),
                      m_status(new QLabel),

                      m\_TAB\_Connect(new A\_BT\_TAB\_DIALOG),
                      
                      //m\_TAB\_Ext(new A\_BT\_TAB\_DIALOG\_EXT),
                      m\_intValidator(new QIntValidator(0, 4000000, this))
                      

                      {

                      here is a snippet of class definition A_BT_TAB_DIALOG *m_TAB_Connect = nullptr; you said that new A_BT_TAB_DIALOG), builds a pointer - to object A_BT_TAB_DIALOG and m_TAB_Connect(new A_BT_TAB_DIALOG), is a function call of function m_TAB_Connect that is not what I thought is happening and I hope it is not some kind of Qt "stuff" I am reading m_TAB_Connect(new A_BT_TAB_DIALOG), as passing pointer m_TAB_Connect of (new) object A_BT_TAB_DIALOG am I wrong? I am not trying to nitpick, but I am having an issue accessing correct instance of the parent class AND NEED to get this terminology correct.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #15

                      Let's try and look at this set of parameters line by line 1. Why are you calling QDialog here? That line of code creates a new QDialog object, but you do not save any pointer to it, so what is the reason for that?

                      QDialog(parent),
                      

                      2. I assume that m_ui is declared as a pointer to a Ui::SettingsDialog which you will refer to somewhere later.

                      m\_ui(new Ui::SettingsDialog),
                      

                      3. As with point 2 you create a new object whose pointer you store in m_status.

                      m\_status(new QLabel),
                      

                      4. Possibly the same as 2 and 3 but I have no idea what m_TAB_Connect is. Is it a pointer, or a method?

                      m\_TAB\_Connect(new A\_BT\_TAB\_DIALOG),
                      

                      5. Same as comment 4.

                      m\_intValidator(new QIntValidator(0, 4000000, this))
                      

                      So without further details of the class it is impossible to say whether this is correct or not. Constructors really should do the minimal initialisation necessary to create the object, that is just setting properties that do not require callouts to other classes. Any calls to other constructors, methods, API functions etc. is generally best left to an initialisation method.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups