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. Call constructor

Call constructor

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

    Hello everyone, Is it legal and good code to call one constructor from another in the same class? I show the pseudo code below.

    Test(bool param1, bool param2)
    {
    // set and manipulate param1

    // call constructor with a single bool input parameter
    
    Test (param2);
    

    }

    Test(bool param2) : active (param2)
    {
    // do something
    }

    thanks in advance, George

    F N 2 Replies Last reply
    0
    • G George_George

      Hello everyone, Is it legal and good code to call one constructor from another in the same class? I show the pseudo code below.

      Test(bool param1, bool param2)
      {
      // set and manipulate param1

      // call constructor with a single bool input parameter
      
      Test (param2);
      

      }

      Test(bool param2) : active (param2)
      {
      // do something
      }

      thanks in advance, George

      F Offline
      F Offline
      fefe wyx
      wrote on last edited by
      #2

      No, you can't do that. It will just create a temporary object, but not calling the other constructor on the same object. You can write your initialization code in a separate function, and call the initialization function from each constructor.

      G 1 Reply Last reply
      0
      • F fefe wyx

        No, you can't do that. It will just create a temporary object, but not calling the other constructor on the same object. You can write your initialization code in a separate function, and call the initialization function from each constructor.

        G Offline
        G Offline
        George_George
        wrote on last edited by
        #3

        Thanks fefe.wyx, Question answered. regards, George

        1 Reply Last reply
        0
        • G George_George

          Hello everyone, Is it legal and good code to call one constructor from another in the same class? I show the pseudo code below.

          Test(bool param1, bool param2)
          {
          // set and manipulate param1

          // call constructor with a single bool input parameter
          
          Test (param2);
          

          }

          Test(bool param2) : active (param2)
          {
          // do something
          }

          thanks in advance, George

          N Offline
          N Offline
          Nemanja Trifunovic
          wrote on last edited by
          #4

          George_George wrote:

          Is it legal and good code to call one constructor from another in the same class?

          Currently no, but the next version of the Standard includes delegating constructors[^] which will enable you to get such functionality, although with a slightly different syntax.

          Programming Blog utf8-cpp

          G 1 Reply Last reply
          0
          • N Nemanja Trifunovic

            George_George wrote:

            Is it legal and good code to call one constructor from another in the same class?

            Currently no, but the next version of the Standard includes delegating constructors[^] which will enable you to get such functionality, although with a slightly different syntax.

            Programming Blog utf8-cpp

            G Offline
            G Offline
            George_George
            wrote on last edited by
            #5

            Thanks Nemanja, I assume in current version of C++ and for common vendors/compilers, like MSVC 2008/2005, it is not supported, right? regards, George

            N 1 Reply Last reply
            0
            • G George_George

              Thanks Nemanja, I assume in current version of C++ and for common vendors/compilers, like MSVC 2008/2005, it is not supported, right? regards, George

              N Offline
              N Offline
              Nemanja Trifunovic
              wrote on last edited by
              #6

              George_George wrote:

              I assume in current version of C++ and for common vendors/compilers, like MSVC 2008/2005, it is not supported, right?

              Right. I bet it would show up in gcc first. Keep an eye on Status of Experimental C++0x Support in GCC 4.3[^]

              Programming Blog utf8-cpp

              G 1 Reply Last reply
              0
              • N Nemanja Trifunovic

                George_George wrote:

                I assume in current version of C++ and for common vendors/compilers, like MSVC 2008/2005, it is not supported, right?

                Right. I bet it would show up in gcc first. Keep an eye on Status of Experimental C++0x Support in GCC 4.3[^]

                Programming Blog utf8-cpp

                G Offline
                G Offline
                George_George
                wrote on last edited by
                #7

                Thanks Nemanja, Question answered. regards, George

                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