1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

placement new operator help

Discussion in 'C++' started by forumuser11, Jul 5, 2010.

  1. forumuser11

    forumuser11 New Member

    Joined:
    Jun 28, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <iostream>
    using namespace std;
    
    class addition 
    {
        int a, b;
    
        public:
          addition() 
          {
                 a=0;
                 b=0;
          }
    
          addition(int x, int y) 
          {
              a = x;
              b = y;
          }
    
          void show() 
          {
              cout << a << " ";
              cout << b << "\n";
          }
    
          addition operator+(addition op2);
    };
    
    addition addition::operator+(addition op2)
    {
         addition temp;
         temp.a = op2.a + a;
         temp.b = op2.b + b;
         return temp;
    }
    
    int main()
    {
        char* buf=new char[100];
         addition* a1=new (buf) addition(1,3);//placement new operator   
         return 0;
    }
    Queries:
    i. Is it possible to create object at a memory location on heap? i.e. let's say memory locaiton 2134433 present on heap....If yes, pl provide the code?

    ii. In the above code, what is the use of placement new opearator as we dont know address of memory location where object is getting creating?
     

Share This Page