C++ Object Creation and Memory Allocation

前端 未结 5 1307
一整个雨季
一整个雨季 2021-01-28 10:14

Let\'s say I have a simple Movie object that just keeps track of a few data members of name, length and cost.

If in my driver file, I create a Movie object. Which is pre

5条回答
  •  醉梦人生
    2021-01-28 11:06

    You can simply instantiate the object like this:

    Movie firstMovie ("Titanic", 126, 13.2);

    You have to use the new keyword when you are initializing raw pointers.

    Movie *firstMovie = new Movie("Titanic", 126, 13.2);

    The difference is that the pointer object uses -> when accessing member functions while the other object uses the . notation. Also the pointer object is allocated in the heap while the other one is allocated in the stack.

提交回复
热议问题