-2

I Was just confused on the part of using a pointer on C++.. well you might say, "a pointer is obviously a memory adress of another variable and there are certaintly conditions in your program where you will need them". But i dont mean pointer in general, i mean the pointer you use to like "simulate" a class... I think code will explain it 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 is always a pointer. 
      Same thing as above, but better practice!
      */

    bookPointer->setBooksId(100);
    std::cout << "POINTER ARROW  : " << bookPointer->getBookId() << std::endl;
    return 0;
}

Here you see i have another pointer that is called bookPointer which all it does is the same as the original instance of book class book1... I dont get it.. What is the advantage of using this? Give me a scenario if you can! Thanks for Helping!!

amanuel2
  • 4,508
  • 4
  • 36
  • 67
  • There is indeed no benefit in the use you have in your example code. But look at the accepted answer to the question linked to by Phil. The answer lists the scenarios where pointers (or references) actually *must* be used. – Peter - Reinstate Monica Apr 21 '16 at 19:33

1 Answers1

1

There is no "simulation" happening at all. book1 has an address too, and the this pointer is set to the address of book1 when you do book1.setBooksId(123);. There is no difference.

doug65536
  • 6,562
  • 3
  • 43
  • 53
  • There is a diffrence according to this: http://stackoverflow.com/questions/22146094/why-should-i-use-a-pointer-rather-than-the-object-itself – amanuel2 Apr 21 '16 at 19:28
  • @AmanuelBogale Yeah? Where? – doug65536 Apr 21 '16 at 19:30
  • The Anwsers i think.. The First Anwser? I Cant see his position on this topic – amanuel2 Apr 21 '16 at 19:32
  • @AmanuelBogale The `this` pointer is just a hidden parameter in the call to the method. Whether the pointer is read from a pointer variable (`foo->bar()`), or inferred by taking the address of a variable (`foo.bar()`), at the end of the day, it passes a pointer to the method. – doug65536 Apr 21 '16 at 19:33
  • @AmanuelBogale If you are being that vague, why not say it is on the internet? – doug65536 Apr 21 '16 at 19:34
  • Oh doug65.. But in the tutorial im watching(since im a begginer been 2 days...) here https://www.youtube.com/watch?v=8FL-fDuASyk&list=PLS1QulWo1RIYSyC6w2-rDssprPrEsgtVK&index=30 .. You can see in the first comment it explains why its good to have the pointer to instance of class.... But i dont really get it! I just dont understand if there is any diffrence, because i would love to know since im trying to code in best practice. Bieng Vague? Im not bieng vague.. – amanuel2 Apr 21 '16 at 19:35
  • Taking the address of a local and calling it through the pointer is IDENTICAL to just using dot notation and calling `book1.method()`. The tutorial is misleading you. – doug65536 Apr 21 '16 at 19:39
  • It Says: Memory management and performance. Passing a pointer to a class object (or large data structure) is MUCH faster than passing the entire class object (or large data structure). Is that actually true? In my oppinon, i dont think thats true as both the pointer and the class object is holding the same memory.... – amanuel2 Apr 21 '16 at 19:39
  • It does not copy anything. Your point is unrelated. As I said already, IDENTICAL TO. – doug65536 Apr 21 '16 at 19:40
  • Ok One More Thing Doug65. What is this trying to say? And is it true? "Also, sometimes a called method needs to instantiate or populate and object/data structure, and that needs to be accessible to other methods in the class. So by having a pointer you can have shared access to that object in a more efficient manner" – amanuel2 Apr 21 '16 at 19:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109851/discussion-between-doug65536-and-amanuel-bogale). – doug65536 Apr 21 '16 at 19:40