about overload : operate new
-
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~~~
-
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~~~
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
-
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
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:)
-
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:)
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- Call the operator
new
(the default or a user-defined version) passing the size of the object to be constructed. new
allocates the memory as it sees fit and returns avoid *
.- 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 - Call the operator