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. What's the difference?

What's the difference?

Scheduled Pinned Locked Moved C / C++ / MFC
question
11 Posts 7 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.
  • T Offline
    T Offline
    tas2826
    wrote on last edited by
    #1

    I am curious about something dealing with constructors in dialog classes. I have seen a certain way of doing the constructor and I wonder if it actually buys anything. Here is what I am talking about: FOO::FOO(CWnd* pParent /*=NULL*/) : CDialog(FOO::IDD, pParent), m_pSomeObject( NULL ), m_bSomeBoolean( false ), m_iSomeInt( 0 ), m_sSomeString( "" ) { //{{AFX_DATA_INIT(FOO) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT } Now m_pSomeObject, m_bSomeBoolean, m_iSomeInt, and m_sSomeString are member variables of the class. So, they are being initialized. Is this method of defining a constructor better than doing it this way? FOO::FOO(CWnd* pParent /*=NULL*/) : CDialog(FOO::IDD, pParent) { //{{AFX_DATA_INIT(FOO) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT m_pSomeObject = NULL; m_bSomeBoolean = false; m_iSomeInt = 0; m_sSomeString = ""; } It is not importaint that it be a dialog class, this is just where I am seeing it used. I am just wondering what the advantages of one over the other are, or if there are advantages? Thanks, Troy

    R D T E O 5 Replies Last reply
    0
    • T tas2826

      I am curious about something dealing with constructors in dialog classes. I have seen a certain way of doing the constructor and I wonder if it actually buys anything. Here is what I am talking about: FOO::FOO(CWnd* pParent /*=NULL*/) : CDialog(FOO::IDD, pParent), m_pSomeObject( NULL ), m_bSomeBoolean( false ), m_iSomeInt( 0 ), m_sSomeString( "" ) { //{{AFX_DATA_INIT(FOO) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT } Now m_pSomeObject, m_bSomeBoolean, m_iSomeInt, and m_sSomeString are member variables of the class. So, they are being initialized. Is this method of defining a constructor better than doing it this way? FOO::FOO(CWnd* pParent /*=NULL*/) : CDialog(FOO::IDD, pParent) { //{{AFX_DATA_INIT(FOO) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT m_pSomeObject = NULL; m_bSomeBoolean = false; m_iSomeInt = 0; m_sSomeString = ""; } It is not importaint that it be a dialog class, this is just where I am seeing it used. I am just wondering what the advantages of one over the other are, or if there are advantages? Thanks, Troy

      R Offline
      R Offline
      Rama Krishna Vavilala
      wrote on last edited by
      #2

      It is better to use the contructor syntax. 1. It is the only way to initialize reference member variables in your class. If you have any. 2. Let's say you have an object that has two contructors defined as

      class A
      {
      public:
      A()
      {
      DoSomeStuff();
      }

      A(int x)
      {
      DoSomeStuff(x);
      }

      A& operator = (int x)
      {
      Cleanup();
      DoSomeStuff(x);
      }
      };

      Now lets say you want to contain an object of this class in another class.

      class B
      {
      private:
      A a;
      };

      You can initialize in two ways

      B::B()
      : a(5)
      {
      }

      or

      B::B()
      {
      a = 5;
      }

      If you are not initializing the object in the constructor initialization list like that in the second case. The object will be initialized using the default constructor i.e. DoSomeStuff will be called and then when the contrsuctor code within the block gets processed the assignment operator will be called. In the first case only the constructor of A that takes int argument gets called.

      T N 2 Replies Last reply
      0
      • R Rama Krishna Vavilala

        It is better to use the contructor syntax. 1. It is the only way to initialize reference member variables in your class. If you have any. 2. Let's say you have an object that has two contructors defined as

        class A
        {
        public:
        A()
        {
        DoSomeStuff();
        }

        A(int x)
        {
        DoSomeStuff(x);
        }

        A& operator = (int x)
        {
        Cleanup();
        DoSomeStuff(x);
        }
        };

        Now lets say you want to contain an object of this class in another class.

        class B
        {
        private:
        A a;
        };

        You can initialize in two ways

        B::B()
        : a(5)
        {
        }

        or

        B::B()
        {
        a = 5;
        }

        If you are not initializing the object in the constructor initialization list like that in the second case. The object will be initialized using the default constructor i.e. DoSomeStuff will be called and then when the contrsuctor code within the block gets processed the assignment operator will be called. In the first case only the constructor of A that takes int argument gets called.

        T Offline
        T Offline
        tas2826
        wrote on last edited by
        #3

        It was not clear to me. So, is it better to do it this way?

        Rama Krishna Vavilala wrote:

        B::B() : a(5) { }

        Or this way?

        Rama Krishna Vavilala wrote:

        B::B() { a = 5; }

        Which one is "Constructor Syntax"?

        N 1 Reply Last reply
        0
        • R Rama Krishna Vavilala

          It is better to use the contructor syntax. 1. It is the only way to initialize reference member variables in your class. If you have any. 2. Let's say you have an object that has two contructors defined as

          class A
          {
          public:
          A()
          {
          DoSomeStuff();
          }

          A(int x)
          {
          DoSomeStuff(x);
          }

          A& operator = (int x)
          {
          Cleanup();
          DoSomeStuff(x);
          }
          };

          Now lets say you want to contain an object of this class in another class.

          class B
          {
          private:
          A a;
          };

          You can initialize in two ways

          B::B()
          : a(5)
          {
          }

          or

          B::B()
          {
          a = 5;
          }

          If you are not initializing the object in the constructor initialization list like that in the second case. The object will be initialized using the default constructor i.e. DoSomeStuff will be called and then when the contrsuctor code within the block gets processed the assignment operator will be called. In the first case only the constructor of A that takes int argument gets called.

          N Offline
          N Offline
          Nish Nishant
          wrote on last edited by
          #4

          Only problem is when you have multiple constructors. So if your initialization list is huge, you need to copy/paste that for every constructor overload. In such cases a better solution might be to use an Init function that's called by all these constructors. Regards, Nish

          My blog : Nish’s thoughts on MFC, C++/CLI and .NET

          1 Reply Last reply
          0
          • T tas2826

            It was not clear to me. So, is it better to do it this way?

            Rama Krishna Vavilala wrote:

            B::B() : a(5) { }

            Or this way?

            Rama Krishna Vavilala wrote:

            B::B() { a = 5; }

            Which one is "Constructor Syntax"?

            N Offline
            N Offline
            Nish Nishant
            wrote on last edited by
            #5

            The former is the proper way to do it (that's called a constructor initialization list). Regards, Nish

            My blog : Nish’s thoughts on MFC, C++/CLI and .NET

            T 1 Reply Last reply
            0
            • N Nish Nishant

              The former is the proper way to do it (that's called a constructor initialization list). Regards, Nish

              My blog : Nish’s thoughts on MFC, C++/CLI and .NET

              T Offline
              T Offline
              tas2826
              wrote on last edited by
              #6

              I think I am seeing this now. The one question I have is does the constructor initialization list buy you anything for primitive data types? It seems a waste for that and they would be better to initialize in the body of a constructor or in an initialization member function.

              1 Reply Last reply
              0
              • T tas2826

                I am curious about something dealing with constructors in dialog classes. I have seen a certain way of doing the constructor and I wonder if it actually buys anything. Here is what I am talking about: FOO::FOO(CWnd* pParent /*=NULL*/) : CDialog(FOO::IDD, pParent), m_pSomeObject( NULL ), m_bSomeBoolean( false ), m_iSomeInt( 0 ), m_sSomeString( "" ) { //{{AFX_DATA_INIT(FOO) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT } Now m_pSomeObject, m_bSomeBoolean, m_iSomeInt, and m_sSomeString are member variables of the class. So, they are being initialized. Is this method of defining a constructor better than doing it this way? FOO::FOO(CWnd* pParent /*=NULL*/) : CDialog(FOO::IDD, pParent) { //{{AFX_DATA_INIT(FOO) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT m_pSomeObject = NULL; m_bSomeBoolean = false; m_iSomeInt = 0; m_sSomeString = ""; } It is not importaint that it be a dialog class, this is just where I am seeing it used. I am just wondering what the advantages of one over the other are, or if there are advantages? Thanks, Troy

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                The former is referred to as an initialization list. One benefit, if you need it, is improved performance. The latter is an assignment, which could cause a temporary object to be created. If the member variables you are initializing are intrinsic types (e.g., int, char), there is no difference in performance.


                "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

                1 Reply Last reply
                0
                • T tas2826

                  I am curious about something dealing with constructors in dialog classes. I have seen a certain way of doing the constructor and I wonder if it actually buys anything. Here is what I am talking about: FOO::FOO(CWnd* pParent /*=NULL*/) : CDialog(FOO::IDD, pParent), m_pSomeObject( NULL ), m_bSomeBoolean( false ), m_iSomeInt( 0 ), m_sSomeString( "" ) { //{{AFX_DATA_INIT(FOO) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT } Now m_pSomeObject, m_bSomeBoolean, m_iSomeInt, and m_sSomeString are member variables of the class. So, they are being initialized. Is this method of defining a constructor better than doing it this way? FOO::FOO(CWnd* pParent /*=NULL*/) : CDialog(FOO::IDD, pParent) { //{{AFX_DATA_INIT(FOO) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT m_pSomeObject = NULL; m_bSomeBoolean = false; m_iSomeInt = 0; m_sSomeString = ""; } It is not importaint that it be a dialog class, this is just where I am seeing it used. I am just wondering what the advantages of one over the other are, or if there are advantages? Thanks, Troy

                  T Offline
                  T Offline
                  tas2826
                  wrote on last edited by
                  #8

                  Okay, I see now. Thank you Rama, Nishant and David for the help. I found another link with a really good explanation too, if you are interested. From a different forum, sorry if that rulles any feathers. http://www.codeguru.com/forum/showpost.php?p=1176852&postcount=6[^] Thanks again for the help. Troy

                  R 1 Reply Last reply
                  0
                  • T tas2826

                    I am curious about something dealing with constructors in dialog classes. I have seen a certain way of doing the constructor and I wonder if it actually buys anything. Here is what I am talking about: FOO::FOO(CWnd* pParent /*=NULL*/) : CDialog(FOO::IDD, pParent), m_pSomeObject( NULL ), m_bSomeBoolean( false ), m_iSomeInt( 0 ), m_sSomeString( "" ) { //{{AFX_DATA_INIT(FOO) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT } Now m_pSomeObject, m_bSomeBoolean, m_iSomeInt, and m_sSomeString are member variables of the class. So, they are being initialized. Is this method of defining a constructor better than doing it this way? FOO::FOO(CWnd* pParent /*=NULL*/) : CDialog(FOO::IDD, pParent) { //{{AFX_DATA_INIT(FOO) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT m_pSomeObject = NULL; m_bSomeBoolean = false; m_iSomeInt = 0; m_sSomeString = ""; } It is not importaint that it be a dialog class, this is just where I am seeing it used. I am just wondering what the advantages of one over the other are, or if there are advantages? Thanks, Troy

                    E Offline
                    E Offline
                    Eric Jacobsen
                    wrote on last edited by
                    #9

                    Scott Meyers, in "Effective C++" says to "Prefer initialization to assignment in constructors" (Item 12) "From a purely pragmatic point of view, there are times when the initialization list must be used. In particular, const and reference members may only be initialized, never assigned." There is also a question of efficiency: "When a member initialization list is used, only a single string member function is called. When assignment inside the constructor is used, two are called. . . Even in the case of the lowly string type, the cost of an unnecessary function call may be significant, and as classes become larger and more complex, so do their constructors, and so does the cost of constructing objects. If you establish the habit of using a member initialization list whenever you can, not only do you satisfy a requirement for const and reference members, you also minimize the chances of initializing data members in an inefficient manner." check out the book for the whole article--good stuff! ---------------------------------------- Please reply in the forum--my email is filtered

                    1 Reply Last reply
                    0
                    • T tas2826

                      I am curious about something dealing with constructors in dialog classes. I have seen a certain way of doing the constructor and I wonder if it actually buys anything. Here is what I am talking about: FOO::FOO(CWnd* pParent /*=NULL*/) : CDialog(FOO::IDD, pParent), m_pSomeObject( NULL ), m_bSomeBoolean( false ), m_iSomeInt( 0 ), m_sSomeString( "" ) { //{{AFX_DATA_INIT(FOO) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT } Now m_pSomeObject, m_bSomeBoolean, m_iSomeInt, and m_sSomeString are member variables of the class. So, they are being initialized. Is this method of defining a constructor better than doing it this way? FOO::FOO(CWnd* pParent /*=NULL*/) : CDialog(FOO::IDD, pParent) { //{{AFX_DATA_INIT(FOO) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT m_pSomeObject = NULL; m_bSomeBoolean = false; m_iSomeInt = 0; m_sSomeString = ""; } It is not importaint that it be a dialog class, this is just where I am seeing it used. I am just wondering what the advantages of one over the other are, or if there are advantages? Thanks, Troy

                      O Offline
                      O Offline
                      Owner drawn
                      wrote on last edited by
                      #10

                      It's mainly used for initializing const variables. This is because what comes on the line with the constructor call gets called first before it enters the constructor body. So if you have any value to initialize (for eg const vars) before any other value is initialized then we do it here. This is the main purpose...

                      Jesus Lives Forever - Amen:rose:

                      --Owner drawn:rose: --An eye for an eye makes the whole world blind. --If you find my post helpful then do rate it. --Jesus is Lord:rose:

                      1 Reply Last reply
                      0
                      • T tas2826

                        Okay, I see now. Thank you Rama, Nishant and David for the help. I found another link with a really good explanation too, if you are interested. From a different forum, sorry if that rulles any feathers. http://www.codeguru.com/forum/showpost.php?p=1176852&postcount=6[^] Thanks again for the help. Troy

                        R Offline
                        R Offline
                        Ryan Binns
                        wrote on last edited by
                        #11

                        Aaaahhhhh.... The forum that Shall Not Be NamedTM :~ There's quite a few members here (including me) who left That Other Place when they sold their soul to the devil... This site was set up initially for that very reason.

                        Ryan

                        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                        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