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. Implicitly calling constructors

Implicitly calling constructors

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++architecturehelptutorial
8 Posts 5 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I have been playing with PC-lint and reading the Scott Meyers Effective/More Effective C++ books and have a question about implicitly calling constructors. This may be a style issue, or it may turn out to be something more important. For example, suppose you have a class that contains a std::string (or CString), and the strings should be empty ("") when the class is created. I have always let the compiler implicitly call the string constructor, and hence would have something that looks like this:

    class foo
    {
    private:
    std::string str_;
    public:
    foo() /* str_ constructor implicitly called */
    {
    }
    };

    However, it has been suggested that all members should appear in the constructors initialization list, and that I should change my constructor to one of the following:

    foo() : str_("")

    or

    foo() : str_()

    Which of the abiove you use? Any solid reasons why? Also, in the case of a string, would you explicitly initialize it to an empty string (str_("")) or call the strings default constructor (str_())?


    Kicking squealing Gucci little piggy.

    N P D L 4 Replies Last reply
    0
    • L Lost User

      I have been playing with PC-lint and reading the Scott Meyers Effective/More Effective C++ books and have a question about implicitly calling constructors. This may be a style issue, or it may turn out to be something more important. For example, suppose you have a class that contains a std::string (or CString), and the strings should be empty ("") when the class is created. I have always let the compiler implicitly call the string constructor, and hence would have something that looks like this:

      class foo
      {
      private:
      std::string str_;
      public:
      foo() /* str_ constructor implicitly called */
      {
      }
      };

      However, it has been suggested that all members should appear in the constructors initialization list, and that I should change my constructor to one of the following:

      foo() : str_("")

      or

      foo() : str_()

      Which of the abiove you use? Any solid reasons why? Also, in the case of a string, would you explicitly initialize it to an empty string (str_("")) or call the strings default constructor (str_())?


      Kicking squealing Gucci little piggy.

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      Rob Caldecott wrote:

      Which of the abiove you use? Any solid reasons why? Also, in the case of a string, would you explicitly initialize it to an empty string (str_("")) or call the strings default constructor (str_())?

      Since str_("") doesn't serve any better purpose that str_() I would go for str_(). Since a good default constructor will set it's member elements to a predefined state. This should suffice. :)


      Nibu thomas A Developer Programming tips[^]  My site[^]

      W 1 Reply Last reply
      0
      • N Nibu babu thomas

        Rob Caldecott wrote:

        Which of the abiove you use? Any solid reasons why? Also, in the case of a string, would you explicitly initialize it to an empty string (str_("")) or call the strings default constructor (str_())?

        Since str_("") doesn't serve any better purpose that str_() I would go for str_(). Since a good default constructor will set it's member elements to a predefined state. This should suffice. :)


        Nibu thomas A Developer Programming tips[^]  My site[^]

        W Offline
        W Offline
        wheelerbarry
        wrote on last edited by
        #3

        calling str_() would save on having to pass a parameter. (not much of a difference, just me being picky).

        1 Reply Last reply
        0
        • L Lost User

          I have been playing with PC-lint and reading the Scott Meyers Effective/More Effective C++ books and have a question about implicitly calling constructors. This may be a style issue, or it may turn out to be something more important. For example, suppose you have a class that contains a std::string (or CString), and the strings should be empty ("") when the class is created. I have always let the compiler implicitly call the string constructor, and hence would have something that looks like this:

          class foo
          {
          private:
          std::string str_;
          public:
          foo() /* str_ constructor implicitly called */
          {
          }
          };

          However, it has been suggested that all members should appear in the constructors initialization list, and that I should change my constructor to one of the following:

          foo() : str_("")

          or

          foo() : str_()

          Which of the abiove you use? Any solid reasons why? Also, in the case of a string, would you explicitly initialize it to an empty string (str_("")) or call the strings default constructor (str_())?


          Kicking squealing Gucci little piggy.

          P Offline
          P Offline
          prasad_som
          wrote on last edited by
          #4

          Rob Caldecott wrote:

          For example, suppose you have a class that contains a std::string (or CString), and the strings should be empty ("") when the class is created. I have always let the compiler implicitly call the string constructor, and hence would have something that looks like this: class foo{private: std::string str_;public: foo() /* str_ constructor implicitly called */ { }};

          you are right.

          Rob Caldecott wrote:

          However, it has been suggested that all members should appear in the constructors initialization list, and that I should change my constructor to one of the following: foo() : str_("") or foo() : str_()

          In this case it doesn't make sense to initialize member variable in initialization list. Because, generally, well designed classes takes care of default initialization. Only difference, here would hapeen is, if you doesn't use member initialization list, default c'tor will be called, and copy c'tor otherwise. It is advised to use member initialization list, because it save assignment operator call; if you are going to assign some value to member variable. consider this scenario,

          class foo
          {
          CStrin m_sMyString;
          public:
          foo()
          {
          m_sMyString = "This string i want as default"; //assignment operator
          //here m_sMyString is initialized to default value,and again,assigned to new
          // value using assignment operator
          }

          };

          where as this is efficient,

          class foo
          {
          CStrin m_sMyString;
          public:
          foo(): m_sMyString("This string i want as default")
          {
          //here m_sMyString is initialized to value we want
          }

          };

          Prasad Notifier using ATL | Operator new[],delete[][^]

          1 Reply Last reply
          0
          • L Lost User

            I have been playing with PC-lint and reading the Scott Meyers Effective/More Effective C++ books and have a question about implicitly calling constructors. This may be a style issue, or it may turn out to be something more important. For example, suppose you have a class that contains a std::string (or CString), and the strings should be empty ("") when the class is created. I have always let the compiler implicitly call the string constructor, and hence would have something that looks like this:

            class foo
            {
            private:
            std::string str_;
            public:
            foo() /* str_ constructor implicitly called */
            {
            }
            };

            However, it has been suggested that all members should appear in the constructors initialization list, and that I should change my constructor to one of the following:

            foo() : str_("")

            or

            foo() : str_()

            Which of the abiove you use? Any solid reasons why? Also, in the case of a string, would you explicitly initialize it to an empty string (str_("")) or call the strings default constructor (str_())?


            Kicking squealing Gucci little piggy.

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

            See member initialization list for more.


            "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

            "Judge not by the eye but by the heart." - Native American Proverb

            L 1 Reply Last reply
            0
            • D David Crow

              See member initialization list for more.


              "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

              "Judge not by the eye but by the heart." - Native American Proverb

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

              OK - the order of initialization is important, etc. but I am still unsure whether it is good practice to initialize all members, even those (like strings for example), that already have default constructors that can be called implicitly. What do you do in your code for example? Do you initialize everything or just POD types?


              Kicking squealing Gucci little piggy.

              D 1 Reply Last reply
              0
              • L Lost User

                I have been playing with PC-lint and reading the Scott Meyers Effective/More Effective C++ books and have a question about implicitly calling constructors. This may be a style issue, or it may turn out to be something more important. For example, suppose you have a class that contains a std::string (or CString), and the strings should be empty ("") when the class is created. I have always let the compiler implicitly call the string constructor, and hence would have something that looks like this:

                class foo
                {
                private:
                std::string str_;
                public:
                foo() /* str_ constructor implicitly called */
                {
                }
                };

                However, it has been suggested that all members should appear in the constructors initialization list, and that I should change my constructor to one of the following:

                foo() : str_("")

                or

                foo() : str_()

                Which of the abiove you use? Any solid reasons why? Also, in the case of a string, would you explicitly initialize it to an empty string (str_("")) or call the strings default constructor (str_())?


                Kicking squealing Gucci little piggy.

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

                Just found mention of this in the popular C++ FAQ Lite which says: "In fact, constructors should initialize as a rule all member objects in the initialization list."[^] Good enough for me. :)


                Kicking squealing Gucci little piggy.

                1 Reply Last reply
                0
                • L Lost User

                  OK - the order of initialization is important, etc. but I am still unsure whether it is good practice to initialize all members, even those (like strings for example), that already have default constructors that can be called implicitly. What do you do in your code for example? Do you initialize everything or just POD types?


                  Kicking squealing Gucci little piggy.

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

                  Rob Caldecott wrote:

                  Do you initialize everything or just POD types?

                  Only those that I know for sure don't have their own initialization.


                  "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                  "Judge not by the eye but by the heart." - Native American Proverb

                  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