c++: what's the difference between new Object() and Object()

后端 未结 3 1506
孤城傲影
孤城傲影 2021-01-07 08:17

so in C++ you can instantiate objects using the new keyword or otherwise...

Object o = new Object();

but you can also just do



        
3条回答
  •  梦谈多话
    2021-01-07 08:39

    I was Searching for a question like above but with variation, so I'll add my question here

    I noticed difference in visual studio 2015 compiler and gcc v4.8.5.

    class Object
    {
    public:
    x=0;
    void display(){ std::cout<<" value of x:   "<display(); // this gives garbage to this->x , uninit value in visualstudio compiler and //in linux gcc it inits this->x to 0
    
    Object *o = new Object(); // works well
    o->display();
    

提交回复
热议问题