In C++ I Cannot Grasp Pointers and Classes

后端 未结 27 1737
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 02:25

I\'m fresh out of college and have been working in C++ for some time now. I understand all the basics of C++ and use them, but I\'m having a hard time grasping more advance

27条回答
  •  执笔经年
    2020-12-03 02:51

    Pointers and classes aren't really advanced topics in C++. They are pretty fundamental.

    For me, pointers solidified when I started drawing boxes with arrows. Draw a box for an int. And int* is now a separate box with an arrow pointing to the int box.

    So:

    int foo = 3;           // integer
    int* bar = &foo;       // assigns the address of foo to my pointer bar
    

    With my pointer's box (bar) I have the choice of either looking at the address inside the box. (Which is the memory address of foo). Or I can manipulate whatever I have an address to. That manipulation means I'm following that arrow to the integer (foo).

    *bar = 5;  // asterix means "dereference" (follow the arrow), foo is now 5
    bar = 0;   // I just changed the address that bar points to
    

    Classes are another topic entirely. There's some books on object oriented design, but I don't know good ones for beginners of the top of my head. You might have luck with an intro Java book.

提交回复
热议问题