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. esoteric use of the "new" keyword

esoteric use of the "new" keyword

Scheduled Pinned Locked Moved C / C++ / MFC
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.
  • D Offline
    D Offline
    Dave Calkins
    wrote on last edited by
    #1

    I came across this use of the "new" keyword in a library recently. I had to look it up to see what was going on. Apparently its valid, I've just never seen it before in any code examples, books, articles, etc. If you write a custom new operator, you can write it to take an extra argument. When callers use the new keyword they supply an additional argument which gets passed to the new operator. So you can call MyClass* instance = new 128 MyClass; This creates an instance of MyClass as expected but when invoking the custom new operator passes along the argument 128. Anyway, I'm just curious if anyone else has seen or used this. It caught me off guard.

    J D 2 Replies Last reply
    0
    • D Dave Calkins

      I came across this use of the "new" keyword in a library recently. I had to look it up to see what was going on. Apparently its valid, I've just never seen it before in any code examples, books, articles, etc. If you write a custom new operator, you can write it to take an extra argument. When callers use the new keyword they supply an additional argument which gets passed to the new operator. So you can call MyClass* instance = new 128 MyClass; This creates an instance of MyClass as expected but when invoking the custom new operator passes along the argument 128. Anyway, I'm just curious if anyone else has seen or used this. It caught me off guard.

      J Offline
      J Offline
      James R Twine
      wrote on last edited by
      #2

      Have not seen it done like that before, but I could see it being used for things like allocating additional memory at the end of the object. Remember how variable-length structures like DDEDATA, PDH_COUNTER_INFO and some device driver ones worked?    Also for custom heap management - I want this object on this heap and that object created on on that heap, or created in a pre-reserved (shared?) block of memory.    Peace!

      -=- James
      Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
      Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
      See DeleteFXPFiles

      1 Reply Last reply
      0
      • D Dave Calkins

        I came across this use of the "new" keyword in a library recently. I had to look it up to see what was going on. Apparently its valid, I've just never seen it before in any code examples, books, articles, etc. If you write a custom new operator, you can write it to take an extra argument. When callers use the new keyword they supply an additional argument which gets passed to the new operator. So you can call MyClass* instance = new 128 MyClass; This creates an instance of MyClass as expected but when invoking the custom new operator passes along the argument 128. Anyway, I'm just curious if anyone else has seen or used this. It caught me off guard.

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

        Dave Calkins wrote:

        Anyway, I'm just curious if anyone else has seen or used this. It caught me off guard.

        Placement new, perhaps?

        "Love people and use things, not love things and use people." - Unknown

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        J D 2 Replies Last reply
        0
        • D David Crow

          Dave Calkins wrote:

          Anyway, I'm just curious if anyone else has seen or used this. It caught me off guard.

          Placement new, perhaps?

          "Love people and use things, not love things and use people." - Unknown

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          J Offline
          J Offline
          James R Twine
          wrote on last edited by
          #4

          Placement new required the use of parenthesis:

          new (value) Type

          Although now that I think about it, you may be correct.  I think that placement new is the only way to pass parameters to new...?    Peace!

          -=- James
          Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
          Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
          See DeleteFXPFiles

          C 1 Reply Last reply
          0
          • J James R Twine

            Placement new required the use of parenthesis:

            new (value) Type

            Although now that I think about it, you may be correct.  I think that placement new is the only way to pass parameters to new...?    Peace!

            -=- James
            Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
            Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
            See DeleteFXPFiles

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            James R. Twine wrote:

            I think that placement new is the only way to pass parameters to new...?

            AFAIK, you can override the new operator. I think that's what is done for the MFC: the file and line are passed to the new operator in order to track memory leaks easier.

            Cédric Moonen Software developer
            Charting control [v1.3]

            J M 2 Replies Last reply
            0
            • C Cedric Moonen

              James R. Twine wrote:

              I think that placement new is the only way to pass parameters to new...?

              AFAIK, you can override the new operator. I think that's what is done for the MFC: the file and line are passed to the new operator in order to track memory leaks easier.

              Cédric Moonen Software developer
              Charting control [v1.3]

              J Offline
              J Offline
              James R Twine
              wrote on last edited by
              #6

              Yep - yer right...

              -=- James
              Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
              Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
              See DeleteFXPFiles

              1 Reply Last reply
              0
              • C Cedric Moonen

                James R. Twine wrote:

                I think that placement new is the only way to pass parameters to new...?

                AFAIK, you can override the new operator. I think that's what is done for the MFC: the file and line are passed to the new operator in order to track memory leaks easier.

                Cédric Moonen Software developer
                Charting control [v1.3]

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                Cedric Moonen wrote:

                AFAIK, you can override the new operator. I think that's what is done for the MFC

                GDI+ does it too :)

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                1 Reply Last reply
                0
                • D David Crow

                  Dave Calkins wrote:

                  Anyway, I'm just curious if anyone else has seen or used this. It caught me off guard.

                  Placement new, perhaps?

                  "Love people and use things, not love things and use people." - Unknown

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  D Offline
                  D Offline
                  Dave Calkins
                  wrote on last edited by
                  #8

                  Correct. As I said, I had to look it up to see what was going on and discovered placement new in the docs. My point was just that I'd never seen this used in code before and was curious if anyone else had. It just seems a bit esoteric :)

                  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