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.
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.
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++..
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----------------------------------
-------------------------------------------------------------------
-------------------------------------------------------------------
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.