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. return; in constructor???

return; in constructor???

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 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.
  • P Offline
    P Offline
    patnsnaudy
    wrote on last edited by
    #1

    Has anyone ever used return in a constructor? Is there anything special about this? I would assume that it skips the execution of the code below and returns a pointer to the object. Is this correct? Thanks, j

    D N M 3 Replies Last reply
    0
    • P patnsnaudy

      Has anyone ever used return in a constructor? Is there anything special about this? I would assume that it skips the execution of the code below and returns a pointer to the object. Is this correct? Thanks, j

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

      While it would be syntactically correct, it doesn't make much sense.


      "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

      P 1 Reply Last reply
      0
      • P patnsnaudy

        Has anyone ever used return in a constructor? Is there anything special about this? I would assume that it skips the execution of the code below and returns a pointer to the object. Is this correct? Thanks, j

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

        patnsnaudy wrote: I would assume that it skips the execution of the code below and returns a pointer to the object. What do you mean by that? Constructors return nothing.


        My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

        P 1 Reply Last reply
        0
        • D David Crow

          While it would be syntactically correct, it doesn't make much sense.


          "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

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

          Thanks for the reply, but I am still not sure. I am reviewing some code and this person uses return in a lot of constructors. But, I am with you.

          1 Reply Last reply
          0
          • N Nemanja Trifunovic

            patnsnaudy wrote: I would assume that it skips the execution of the code below and returns a pointer to the object. What do you mean by that? Constructors return nothing.


            My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

            P Offline
            P Offline
            patnsnaudy
            wrote on last edited by
            #5

            ctor::ctor() { ... do some stuff ... if(true) { return; } ... do some more stuff ... } I assume that "do some more stuff" would not get executed.

            A N 2 Replies Last reply
            0
            • P patnsnaudy

              Has anyone ever used return in a constructor? Is there anything special about this? I would assume that it skips the execution of the code below and returns a pointer to the object. Is this correct? Thanks, j

              M Offline
              M Offline
              Michael P Butler
              wrote on last edited by
              #6

              All it will do is prevent the execution of code after the return statement. I consider it a bad practise, but I have seen developers use it whilst debugging. Michael CP Blog [^]

              P 1 Reply Last reply
              0
              • P patnsnaudy

                ctor::ctor() { ... do some stuff ... if(true) { return; } ... do some more stuff ... } I assume that "do some more stuff" would not get executed.

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

                patnsnaudy wrote: I assume that "do some more stuff" would not get executed. Correct! Since if (true) is always true. it will return without executing "do some more stuff" 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
                • M Michael P Butler

                  All it will do is prevent the execution of code after the return statement. I consider it a bad practise, but I have seen developers use it whilst debugging. Michael CP Blog [^]

                  P Offline
                  P Offline
                  patnsnaudy
                  wrote on last edited by
                  #8

                  That's the answer I was looking for. You win the prize :beer:

                  1 Reply Last reply
                  0
                  • P patnsnaudy

                    ctor::ctor() { ... do some stuff ... if(true) { return; } ... do some more stuff ... } I assume that "do some more stuff" would not get executed.

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

                    That's correct. But you also wrote and returns a pointer to the object, which is not true.


                    My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

                    P 1 Reply Last reply
                    0
                    • N Nemanja Trifunovic

                      That's correct. But you also wrote and returns a pointer to the object, which is not true.


                      My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

                      P Offline
                      P Offline
                      patnsnaudy
                      wrote on last edited by
                      #10

                      I guess that I meant when you new the object that a pointer to the object would be passed back. The one thing that I thought return in a ctor may do is pass back null and not create the object so: ctor a = new ctor(); // thought that it was possible that a would be null if ctor called return, but now I know that is not the case. I could have tested it, but didn't want to spend the time. :-O

                      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