0

hi all..
Can any one explain, when i have class Pet, what is the difference of declaring variables like each of following.
Pet mypet;
Pet mypet = new pet();
Pet* mypet = new Pet();

thanks.

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by invisal
0

new keyword is used for dynamic memory allocation..that's all i see
i have seen the second type of declaration in java....haven't seen in c++..

0

Here is my understanding; however, I am un-sure if it's correct.

Pet mypet;

You declare mypet as Pet, so the computer will reserve a suitable memory for your variable.

-------------------------------------------------------------------
-----0000000000000000000000000000----------------------------------
-------------------------------------------------------------------
-------------------------------------------------------------------
Pet mypet = new Pet();

You declare mypet as Pet, and you allocate another memory for storing Pet;

-------------------------------------------------------------------
-----0000000000000000000000000000----------------------------------
-------------------------------------------------------------------
-------------0000000000000000000000000000--------------------------
Pet* mypet = new Pet();

You make a pointer and point it to the memory which you've allocated.

---------------------010011010-------------------------------------
-----0000000000000000000000000000----------------------------------
-------------------------------------------------------------------
-------------------------------------------------------------------
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.