In C++ I Cannot Grasp Pointers and Classes

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

    Pointers are not some sort of magical stuff, you're using them all the time!
    When you say:

    int a;

    and the compiler generates storage for 'a', you're practically saying that you're declaring
    an int and you want to name its memory location 'a'.

    When you say:

    int *a;

    you're declaring a variable that can hold a memory location of an int. It's that simple. Also, don't be scared about pointer arithmetics, just always have in mind a "memory map" when you're dealing with pointers and think in terms of walking through memory addresses.

    Classes in C++ are just one way of defining abstract data types. I'd suggest reading a good OOP book to understand the concept, then, if you're interested, learn how C++ compilers generate code to simulate OOP. But this knowledge will come in time, if you stick with C++ long enough :)

    0 讨论(0)
  • 2020-12-03 03:01

    Learn assembly language and then learn C. Then you will know what the underlying principles of machine are (and thefore pointers).

    Pointers and classes are fundamental aspects of C++. If you don't understand them then it means that you don't really understand C++.

    Personally I held back on C++ for several years until I felt I had a firm grasp of C and what was happening under the hood in assembly language. Although this was quite a long time ago now I think it really benefited my career to understand how the computer works at a low-level.

    Learning to program can take many years, but you should stick with it because it is a very rewarding career.

    0 讨论(0)
  • 2020-12-03 03:01

    In a sense, you can consider "pointers" to be one of the two most fundamental types in software - the other being "values" (or "data") - that exist in a huge block of uniquely-addressable memory locations. Think about it. Objects and structs etc don't really exist in memory, only values and pointers do. In fact, a pointer is a value too....the value of a memory address, which in turn contains another value....and so on.

    So, in C/C++, when you declare an "int" (intA), you are defining a 32bit chunk of memory that contains a value - a number. If you then declare an "int pointer" (intB), you are defining a 32bit chunk of memory that contains the address of an int. I can assign the latter to point to the former by stating "intB = &intA", and now the 32bits of memory defined as intB, contains an address corresponding to intA's location in memory.

    When you "dereference" the intB pointer, you are looking at the address stored within intB's memory, finding that location, and then looking at the value stored there (a number).

    Commonly, I have encountered confusion when people lose track of exactly what it is they're dealing with as they use the "&", "*" and "->" operators - is it an address, a value or what? You just need to keep focused on the fact that memory addresses are simply locations, and that values are the binary information stored there.

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