I'm not cubbi, but I'll try to answer these anyway:
NullInfinity wrote: |
---|
Nameless? So rect and trgl aren't the names of the pointer objects? Then why their juxtaposition? |
I think cubbi was being a bit hypertechnical, and it might have thrown you off of what's conceptually happening (which is simple).
rect
and
trgl
are objects.
&rect
and
&trgl
are pointers to those objects.
ppoly1
and
ppoly2
are not CPolygon objects, but rather are just pointers.
Once they have had &rect assigned to them, they point to the
rect
object.
Does "initializing" in this case mean the same thing as explicitly assigning a value for the first time, to a newly created object? Or is there a subtler context I'm missing? |
There are subtleties in the difference between "initialization" and "construction" which still trip me up after well over a decade. In practice, they come up extremely rarely. Unless you're planning on writing a C++ compiler it likely will not matter to you what those differences are.
So yeah... "initialization" here just means assigning the value for the first time.
So, for confirmation: Does this mean I'm initializing the ppoly pointer objects (as opposed to the ppoly Cpolygon objects, which you go on to tell me are separate objects) with the address of rect and trgl, and not any of its member values or return values? |
For starters... there
IS NO ppoly CPolygon object. ppoly is a pointer, it is not an object. It only contains an address. At that address, there is the
CRectangle
object (which, since CRectangle is derived from CPolygon, it means there is also a CPolygon object at that address).
So to recap, there are only 2 objects here: rect and trgl. ppoly1 and ppoly2 are not objects... but they merely point to existing objects.
CPolygon * ppoly2 = &trgl; //assigns the address of trgl to the pointer object, |
Your comment above is correct.
CPolygon & rpoly2 = trgl; //assigns trgl's contents to the reference object, |
Your comment above is incorrect.
References are similar to pointers in that they are not objects, but simply refer to existing objects. This code is practically identical to the previous code with only some minor syntactic differences.
References are conceptually "aliases" or "alternative names" for an existing object. In this case... the object is trgl... and by creating a reference rpoly2, we are saying "rpoly2 is just another name for the trgl object".
The object that's pointed to by ppoly1 (the pointer ppoly1, not the CPolygon, right?) is a CRectangle. The object pointed to by ppoly2 (the pointer ppoly2, not the CPolygon, right?) is a CTriangle.
Questions in bold. |
There are ONLY pointers. Remember that ppoly and ppoly2 do not have CPolygon objects themselves... they are merely pointing to rect and trgl's objects.
Not to be nitpicky, but I thought understanding low-level memory stuff was key to optimization and other things that would separate me as a professional (assuming I am to ever become one). I kinda want to get hung up on this stuff because it's important, and it pertains to general programming principles that go beyond just c++. |
The concepts are more important than the details. Especially since the details can vary from system to system and compiler to compiler.
The first four statements of main(), when compiled and built, all return 4, meaning 4 bytes. Which is the standard size of an integer. This is obvious for the first two, but when I did the same thing for bobby and louanne, which represent peggy and hank's addressses, I get 4 also.
Could you tell me why this is? |
You're looking at it at too low of a level, and its obstructing the conceptual view. This is why you need to take a step back and focus less on the details and more on the concepts. Just because sizeof(int) and sizeof(int*) are both 4 does not mean they are the same. They are conceptually
very different. And the language also treats them very different.
Case in point: sizeof(float) will probably give you the same result as sizeof(int) (both 4).. but clearly floats and ints are different. So using sizeof to determine whether or not two types are the same (or even similar) is nonsense.
Computers are digital devices. This means
EVERYTHING is represented as a series of 0s and 1s.
- A single 'bit' can have a value of either 0 or 1
- A byte consists of 8 bits
- Variables consist of one or more bytes
Even complex types like strings are just a bunch of bytes strung together.
So by that definition...
anything could be called an "integer", but if you do that, the term loses all meaning because it literally defines any and all possible types on a digital machine.