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. about overload : operate new

about overload : operate new

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
4 Posts 2 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.
  • 1 Offline
    1 Offline
    1 5kg
    wrote on last edited by
    #1

    I want to get this code: // --> class A { public: A(int a) { m_a = a; } public: int m_a; }; FILE *fp = fopen("C:\\test.txt", "wt"); A* obj = new (fp) A(10); fclose(fp); // <-- When this code run over, the C:\test.txt file will be written in 10. Just as A(10). In other words, the operate new Function can save A(10) before A construct function implement. I can't solve it! My project have the same codes, but I have no sourcecode. I want to know how to do? I need your help~~~~ Thanks a lot~~~

    J 1 Reply Last reply
    0
    • 1 1 5kg

      I want to get this code: // --> class A { public: A(int a) { m_a = a; } public: int m_a; }; FILE *fp = fopen("C:\\test.txt", "wt"); A* obj = new (fp) A(10); fclose(fp); // <-- When this code run over, the C:\test.txt file will be written in 10. Just as A(10). In other words, the operate new Function can save A(10) before A construct function implement. I can't solve it! My project have the same codes, but I have no sourcecode. I want to know how to do? I need your help~~~~ Thanks a lot~~~

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      What is the intention of your program? If you want to write the integer 10 to a file, then your attempt is way off target. In the expression

      A* obj = new (fp) A(10);

      you are (mistankely) using the so-called placement new, which has nothing to do with writing to a file. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      1 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        What is the intention of your program? If you want to write the integer 10 to a file, then your attempt is way off target. In the expression

        A* obj = new (fp) A(10);

        you are (mistankely) using the so-called placement new, which has nothing to do with writing to a file. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        1 Offline
        1 Offline
        1 5kg
        wrote on last edited by
        #3

        I know i should overload this operater new, such as: void* operater new(size_t n, FILE *fp) { // some code } Now I want to get this object A(10) in the operater new function, so i can save this object's value. But as you know, the construct function always be implemented after operate new funtction, How do I now? My English is poor, please never mind:)

        J 1 Reply Last reply
        0
        • 1 1 5kg

          I know i should overload this operater new, such as: void* operater new(size_t n, FILE *fp) { // some code } Now I want to get this object A(10) in the operater new function, so i can save this object's value. But as you know, the construct function always be implemented after operate new funtction, How do I now? My English is poor, please never mind:)

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          OK, now I see what you are trying to do. I am afraid your idea is not realizable in C++. Overloads of new cannot have access to the object being constructed because construction takes place later. The sequence of operations performed by the compiler is

          1. Call the operator new (the default or a user-defined version) passing the size of the object to be constructed.
          2. new allocates the memory as it sees fit and returns a void *.
          3. The compiler uses this pointer to lay out the object, calling then its constructor.

          So, by the time your overloaded new is called, the object does not even exist. I don't think you can workaround this. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          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