What is the benefit of using a pointer C++ -
this question has answer here:
- why should use pointer rather object itself? 21 answers
i confused on part of using pointer on c++.. might say, "a pointer memory adress of variable , there certaintly conditions in program need them". dont mean pointer in general, mean pointer use "simulate" class... think code explain more:
#include <iostream> #include <string> #include "book.h" int main() { book book1; book *bookpointer = &book1; book1.setbooksid(123); std::cout << "book id: " << book1.getbookid() << std::endl; (*bookpointer).setbooksid(300); std::cout << (*bookpointer).getbookid() << std::endl; /*when usage of arrow member selection member, left pointer. same thing above, better practice! */ bookpointer->setbooksid(100); std::cout << "pointer arrow : " << bookpointer->getbookid() << std::endl; return 0; }
here see have pointer called bookpointer same original instance of book class book1... dont it.. advantage of using this? give me scenario if can! helping!!
there no "simulation" happening @ all. book1
has address too, , this
pointer set address of book1
when book1.setbooksid(123);
. there no difference.
Comments
Post a Comment