C++ pointer to objects

前端 未结 7 590
野趣味
野趣味 2020-12-07 16:19

In C++ do you always have initialize a pointer to an object with the new keyword?

Or can you just have this too:

MyClass *myclass;

mycl         


        
7条回答
  •  南笙
    南笙 (楼主)
    2020-12-07 16:32

    if you want the object on the stack, try this:

    MyClass myclass;
    myclass.DoSomething();
    

    If you need a pointer to that object:

    MyClass* myclassptr = &myclass;
    myclassptr->DoSomething();
    

提交回复
热议问题