C++ pointer to objects

前端 未结 7 589
野趣味
野趣味 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:31

    Simple solution for cast pointer to object

    Online demo

    class myClass
    {
      public:
      void sayHello () {
        cout << "Hello";
      }
    };
    
    int main ()
    {
      myClass* myPointer;
      myClass myObject = myClass(* myPointer); // Cast pointer to object
      myObject.sayHello();
    
      return 0;
    }
    

提交回复
热议问题