In C++ I Cannot Grasp Pointers and Classes

后端 未结 27 1614
隐瞒了意图╮
隐瞒了意图╮ 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:36

    This link has a video describing how pointers work, with claymation. Informative, and easy to digest.

    This page has some good information on the basic of classes.

    0 讨论(0)
  • 2020-12-03 02:38

    The book that cracked pointers for me was Illustrating Ansi C by Donald Alcock. Its full of hand-drawn-style box and arrow diagrams that illustrate pointers, pointer arithmetic, arrays, string functions etc...

    Obviously its a 'C' book but for core fundamentals its hard to beat

    0 讨论(0)
  • 2020-12-03 02:38

    Pretend a pointer is an array address.

    x = 500; // memory address for hello;
    MEMORY[x] = "hello"; 
    print  MEMORY[x]; 
    

    its a graphic oversimplification, but for the most part as long as you never want to know what that number is or set it by hand you should be fine.

    Back when I understood C I had a few macros I had which more or less permitted you to use pointers just like they were an array index in memory. But I've long since lost that code and long since forgotten.

    I recall it started with

    #define MEMORY 0; 
    #define MEMORYADDRESS( a ) *a;
    

    and that on its own is hardly useful. Hopefully somebody else can expand on that logic.

    0 讨论(0)
  • 2020-12-03 02:39

    Pointers already seem to be addressed (no pun intended) in other answers.

    Classes are fundamental to OO. I had tremendous trouble wrenching my head into OO - like, ten years of failed attempts. The book that finally helped me was Craig Larman's "Applying UML and Patterns". I know it sounds as if it's about something different, but it really does a great job of easing you into the world of classes and objects.

    0 讨论(0)
  • 2020-12-03 02:39

    To understand pointers, I can't recommend the K&R book highly enough.

    0 讨论(0)
  • 2020-12-03 02:40

    There's no substiture for practicing.

    It's easy to read through a book or listen to a lecture and feel like you're following what's going on.

    What I would recommend is taking some of the code examples (I assume you have them on disk somewhere), compile them and run them, then try to change them to do something different.

    • Add another subclass to a hierarchy
    • Add a method to an existing class
    • Change an algorithm that iterates forward through a collection to go backward instead.

    I don't think there's any "silver bullet" book that's going to do it.

    For me, what drove home what pointers meant was working in assembly, and seeing that a pointer was actually just an address, and that having a pointer didn't mean that what it pointed to was a meaningful object.

    0 讨论(0)
提交回复
热议问题