Please make me a new object, called xaxis. The type of xaxis , the new object I want to make, is a pointer to a TAxis object. Please make my new object xaxis equal to what you get back from calling the function GetXaxis on the object pointed to by the pointer named h.
In the posted code, xaxis is a pointer to a structure/variable of type TAxis. xaxis is initialized with the returning value of GetXaxis( ). h is another pointer. h's type is known to us as we can't see it's definition/declaration.
Thank you for your responses.
I have read the documention for pointers in cplusplus, but I still feel lost when i want to read and understand another code:(
I have a question from Moschops, isn't it like that to give an object an identity we should use new
So I mean the line above is only a declaration, not making the object xaxis, am I right?
Thank you again
You only use new if you want to dynamically allocate your instance. If you do dynamically allocate a instance of TArrow, all members of that instance are dynamically allocated as well. If the instance isn't dynamically allocated, the instance is created on the program's stack.
This makes an object of type TArrow, named a: TArrow a;
A complete, fully made instance of a TArrow object, made on the piece of memory known as the stack. No need for new unless you want to make it on the heap (as a general rule, make it on the stack when you can, and on the heap when you must).
The thing that make me confuse is that if the pointer stack_ptr to the Quad class gets the memory address of the object stack_quad or the value in that memory address?
The Heap[1] refers to the main memory. When we allocate a block of memory with new, malloc( ), or calloc( ), a specified amount of memory is taken from the heap. The heap is said to be slower than the stack.
The stack[2] is a FILO( First In Last Out ) structure that has a default size of 1MB( 1024-Bytes ). Every program has one. Each object that is created is stored on the stack.
if the pointer stack_ptr to the Quad class gets the memory address of the object stack_quad or the value in that memory address?
When a pointer points to another object of the type, it points to the address of the object it was assigned to. When we dereference a pointer using the asterisk operator, we access the value stored in the address the pointer is pointing to.
A Quad pointer points to a Quad class, not the data members in it. You *could* make an int pointer to a Quad class if the first data member in the class was an int, but don't do that...some variables are private, and for a reason.
in this case stack_ptr is a pointer to the class Quad (points to the address of Quad)
No. stack_ptr is an instance of Quad. stack_ptr can only point to another instance of Quad.
bbcc wrote:
in this case stack_ptr is a dereference operator, returns the value of strack_quad
No. In this case, you're creating an instance of Quad, and initializing it by making it point to the address of strack_quad. NULL is another initializing value of a pointer, which basically means the pointer is pointing to nothing. When you dereference a pointer, you reveal the value the pointer is pointing to. A structure cannot contain a value. A structure holds members. The members of a structure hold the values.
bbcc wrote:
what about this case then? I think it does the same as number 2
This will generate a compiler error. You forgot the address-of( & ) operator before strack_quad.