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. beginner: initializing objects in constructor?

beginner: initializing objects in constructor?

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionlearning
8 Posts 2 Posters 0 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.
  • 7 Offline
    7 Offline
    7stud
    wrote on last edited by
    #1

    Hi, I'm doing the beginning VC++ Dialog Box tutorials on this site, and I noticed when I add a member variable to a control(using the Class Wizard) and select Category: Value, the Wizard adds an initialization line in the Dialog constructor. However, when I add a member variable and select Category: Control with say, Variable Type: CListBox, the Wizard doesn't add anything to the Dialog constructor. Shouldn't the class objects that are members of the class also get initialized in the Dialog constructor? If the answer is yes, how would I do that?

    A 1 Reply Last reply
    0
    • 7 7stud

      Hi, I'm doing the beginning VC++ Dialog Box tutorials on this site, and I noticed when I add a member variable to a control(using the Class Wizard) and select Category: Value, the Wizard adds an initialization line in the Dialog constructor. However, when I add a member variable and select Category: Control with say, Variable Type: CListBox, the Wizard doesn't add anything to the Dialog constructor. Shouldn't the class objects that are members of the class also get initialized in the Dialog constructor? If the answer is yes, how would I do that?

      A Offline
      A Offline
      Antony M Kancidrowski
      wrote on last edited by
      #2

      No you do not need to initialize other classes within the dialog class. They take care of their own initialization. If you take a look at the header file for the dialog you will see a line like

      CListBox m_ListBoxControl;

      This instantiates a CListBox object and calls its constructor. Similarly, when you create and launch a dialog

      CMyDialog myDialog;

      This instantiates the CMyDialog object and calls its constructor

      myDialog.DoModal();

      calls the dialog DoModal function. Ant. I'm hard, yet soft.
      I'm coloured, yet clear.
      I'm fruity and sweet.
      I'm jelly, what am I? Muse on it further, I shall return!
      - David Williams (Little Britain)

      7 1 Reply Last reply
      0
      • A Antony M Kancidrowski

        No you do not need to initialize other classes within the dialog class. They take care of their own initialization. If you take a look at the header file for the dialog you will see a line like

        CListBox m_ListBoxControl;

        This instantiates a CListBox object and calls its constructor. Similarly, when you create and launch a dialog

        CMyDialog myDialog;

        This instantiates the CMyDialog object and calls its constructor

        myDialog.DoModal();

        calls the dialog DoModal function. Ant. I'm hard, yet soft.
        I'm coloured, yet clear.
        I'm fruity and sweet.
        I'm jelly, what am I? Muse on it further, I shall return!
        - David Williams (Little Britain)

        7 Offline
        7 Offline
        7stud
        wrote on last edited by
        #3

        Hi, Thanks for responding. I’m having trouble verifying your explanation. As far as I can tell, a declaration of a member object in a class doesn’t instantiate the member object and call it's constructor. The code below demonstrates that. I declared a class Car with one member object of type Age: #include using namespace std; class Age { public: int years; Age() { cout<<"Age constructor called"; } }; class Car { public: Age a; }; int main() { cout<<"Was a constructor message displayed?"<

        A 1 Reply Last reply
        0
        • 7 7stud

          Hi, Thanks for responding. I’m having trouble verifying your explanation. As far as I can tell, a declaration of a member object in a class doesn’t instantiate the member object and call it's constructor. The code below demonstrates that. I declared a class Car with one member object of type Age: #include using namespace std; class Age { public: int years; Age() { cout<<"Age constructor called"; } }; class Car { public: Age a; }; int main() { cout<<"Was a constructor message displayed?"<

          A Offline
          A Offline
          Antony M Kancidrowski
          wrote on last edited by
          #4

          You need to construct an object of class Car in order for an object of class Age to be constructed. You have missed this out! Ant. I'm hard, yet soft.
          I'm coloured, yet clear.
          I'm fruity and sweet.
          I'm jelly, what am I? Muse on it further, I shall return!
          - David Williams (Little Britain)

          7 1 Reply Last reply
          0
          • A Antony M Kancidrowski

            You need to construct an object of class Car in order for an object of class Age to be constructed. You have missed this out! Ant. I'm hard, yet soft.
            I'm coloured, yet clear.
            I'm fruity and sweet.
            I'm jelly, what am I? Muse on it further, I shall return!
            - David Williams (Little Britain)

            7 Offline
            7 Offline
            7stud
            wrote on last edited by
            #5

            This is what you said: ------- "If you take a look at the header file for the dialog you will see a line like CListBox m_ListBoxControl; This instantiates a CListBox object and calls its constructor." ------- My test code shows that your statement is incorrect--just declaring a member object in a .h file does not instantiate the object and call it's constructor. Now, you seem to be saying something different. Anyway, as far as I can tell, the class Wizard never instantiates the object in a .cpp file with a line like: CListBox m_ListBoxControl; which would create the object and call it's default constructor. But, I don't think a statement like that is appropriate for a class member anyways--I think object members of a class get instantiated when an object of the class is created. I do see this line: CCodeProject_Dialog2Dlg dlg; in CCodeProject_Dialog2App::InitInstance(). I guess that must call the default contructor of the object m_ListBoxControl because it's a member of the CCodeProject_Dialog2Dlg class, and in the creation of 'dlg' it gets called somehow. However, looking around the files, now I don't see the default constructor that's called for 'dlg': CCodeProject_Dialog2Dlg dlg; The only constructor I see is: ------- CCodeProject_Dialog2Dlg::CCodeProject_Dialog2Dlg(CWnd* pParent /*=NULL*/) : CDialog(CCodeProject_Dialog2Dlg::IDD, pParent) ------- which has one paramemter, so it can't be the defautlt constructor, and if the programmer defines a constructor, then they have to provide their own default constructor. Where is the default constructor that's called?

            A 1 Reply Last reply
            0
            • 7 7stud

              This is what you said: ------- "If you take a look at the header file for the dialog you will see a line like CListBox m_ListBoxControl; This instantiates a CListBox object and calls its constructor." ------- My test code shows that your statement is incorrect--just declaring a member object in a .h file does not instantiate the object and call it's constructor. Now, you seem to be saying something different. Anyway, as far as I can tell, the class Wizard never instantiates the object in a .cpp file with a line like: CListBox m_ListBoxControl; which would create the object and call it's default constructor. But, I don't think a statement like that is appropriate for a class member anyways--I think object members of a class get instantiated when an object of the class is created. I do see this line: CCodeProject_Dialog2Dlg dlg; in CCodeProject_Dialog2App::InitInstance(). I guess that must call the default contructor of the object m_ListBoxControl because it's a member of the CCodeProject_Dialog2Dlg class, and in the creation of 'dlg' it gets called somehow. However, looking around the files, now I don't see the default constructor that's called for 'dlg': CCodeProject_Dialog2Dlg dlg; The only constructor I see is: ------- CCodeProject_Dialog2Dlg::CCodeProject_Dialog2Dlg(CWnd* pParent /*=NULL*/) : CDialog(CCodeProject_Dialog2Dlg::IDD, pParent) ------- which has one paramemter, so it can't be the defautlt constructor, and if the programmer defines a constructor, then they have to provide their own default constructor. Where is the default constructor that's called?

              A Offline
              A Offline
              Antony M Kancidrowski
              wrote on last edited by
              #6

              7stud wrote: My test code shows that your statement is incorrect You are not going to get code to execute if you are not using it! In the context of your original question I thought I had explained that you do not need to initialize the composite class objects. You do need to instantiate the class that holds these composites however. Sorry if you did not understand this. When a class is instantiated then all other composite classes within that class are instantiated, if they are defined in the manner discussed earlier. With regards to the Dialog class constructor. CCodeProject_Dialog2Dlg dlg; Does calls the constructor CCodeProject_Dialog2Dlg::CCodeProject_Dialog2Dlg(CWnd* pParent /*=NULL*/) : CDialog(CCodeProject_Dialog2Dlg::IDD, pParent) NOTE: When the header has 1 parameter that is defaulted (in this case to NULL) you do not need to pass that parameter as long as you want the default behaviour. Ant. I'm hard, yet soft.
              I'm coloured, yet clear.
              I'm fruity and sweet.
              I'm jelly, what am I? Muse on it further, I shall return!
              - David Williams (Little Britain)

              7 1 Reply Last reply
              0
              • A Antony M Kancidrowski

                7stud wrote: My test code shows that your statement is incorrect You are not going to get code to execute if you are not using it! In the context of your original question I thought I had explained that you do not need to initialize the composite class objects. You do need to instantiate the class that holds these composites however. Sorry if you did not understand this. When a class is instantiated then all other composite classes within that class are instantiated, if they are defined in the manner discussed earlier. With regards to the Dialog class constructor. CCodeProject_Dialog2Dlg dlg; Does calls the constructor CCodeProject_Dialog2Dlg::CCodeProject_Dialog2Dlg(CWnd* pParent /*=NULL*/) : CDialog(CCodeProject_Dialog2Dlg::IDD, pParent) NOTE: When the header has 1 parameter that is defaulted (in this case to NULL) you do not need to pass that parameter as long as you want the default behaviour. Ant. I'm hard, yet soft.
                I'm coloured, yet clear.
                I'm fruity and sweet.
                I'm jelly, what am I? Muse on it further, I shall return!
                - David Williams (Little Britain)

                7 Offline
                7 Offline
                7stud
                wrote on last edited by
                #7

                NOTE: When the header has 1 parameter that is defaulted (in this case to NULL) you do not need to pass that parameter as long as you want the default behaviour. How is the one parameter defaulted to NULL? If I write a constructor like this: CSomeClass::CSomeClass(a, b, c, d /*All parameters in every function in this project are NULL*/) {} Do all the parameters of every function in the project have default values of NULL? NEVER MIND...I SEE THAT THE DEFAULT VALUE FOR THE SINGLE PARAMETER IS IN THE HEADER FILE. Thanks for your help. :)

                A 1 Reply Last reply
                0
                • 7 7stud

                  NOTE: When the header has 1 parameter that is defaulted (in this case to NULL) you do not need to pass that parameter as long as you want the default behaviour. How is the one parameter defaulted to NULL? If I write a constructor like this: CSomeClass::CSomeClass(a, b, c, d /*All parameters in every function in this project are NULL*/) {} Do all the parameters of every function in the project have default values of NULL? NEVER MIND...I SEE THAT THE DEFAULT VALUE FOR THE SINGLE PARAMETER IS IN THE HEADER FILE. Thanks for your help. :)

                  A Offline
                  A Offline
                  Antony M Kancidrowski
                  wrote on last edited by
                  #8

                  It is a good thing that you are questioning what people tell you. Just to clarify. If you write something like that in the .cpp file it is as a reminder that the parameter is defaulted to NULL CCodeProject_Dialog2Dlg::CCodeProject_Dialog2Dlg(CWnd* pParent /*=NULL*/) If you look in the .h file you will see the constructor defined something like CCodeProject_Dialog2Dlg(CWnd* pParent = NULL) Note if you have more than one parameter, all parameters after the first one that you default must have default values. i.e These are OK Function(int *A = NULL, int *B = NULL, int *C = NULL) Function(int *A, int *B = NULL, int *C = NULL) Function(int *A, int *B, int *C = NULL) These are not Function(int *A = NULL, int *B, int *C = NULL) Function(int *A = NULL, int *B, int *C) Function(int *A, int *B = NULL, int *C) 7stud wrote: Thanks for your help. Your welcome Ant. I'm hard, yet soft.
                  I'm coloured, yet clear.
                  I'm fruity and sweet.
                  I'm jelly, what am I? Muse on it further, I shall return!
                  - David Williams (Little Britain)

                  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