pointers

Could any one help me with the meaning of this:

TAxis *xaxis = h->GetXaxis();

Many thanks in advance
It says:

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.
Last edited on
closed account (zb0S216C)
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.

You can read upon pointers on this website[1].

References:
[1]http://www.cplusplus.com/doc/tutorial/pointers/

Wazzak
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
closed account (zb0S216C)
If a pointer is used to allocate a block of memory with new, the pointer would look like this:

1
2
// Allocate a block of memory which is the same size of TAxis.
TAxis *xaxis( new TAxis( h->GetXaxis( ) ) );


Wazzak
o.k. I think I should be more specific in my question,

I meant if we have already defined a class :

1
2
3
class TArrow{
int ArrowHeadSize;
void Draw();
}

and then I say:

TArrow a;

Does it mean that object a is being built from that class TArrow or I need to use the new methos to make an instance of my class?
Many thanks again
closed account (zb0S216C)
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.

Wazzak
Last edited on
To reiterate m'learned chum Framework;

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).
Last edited on
Could you please tell me more about Stack and Heap part of the memory.

As far as I know, heap relates mostly to the pointers and if we want to give more permanence to an object we intrduce it in heap.

Thanks again
I have another confusion as well that I really apprecite if you help me with that as well:

Here I have a class with the name Quad


1
2
Quad stack_quad(1,2,3);
Quad  *stack_ptr = &strack_quad


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?

Thanks
closed account (zb0S216C)
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.

References:
[1]http://en.wikipedia.org/wiki/Dynamic_memory_allocation
[2]http://en.wikipedia.org/wiki/Stack_(data_structure)

Wazzak
Last edited on
closed account (zb0S216C)
bbcc wrote:
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.

Wazzak
Last edited on
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.
To Framework, So you mean that:

1.Quad *stack_ptr
in this case stack_ptr is a pointer to the class Quad (points to the address of Quad)

2.Quad *stack_ptr = &strack_quad
in this case stack_ptr is a dereference operator, returns the value of strack_quad

3.Quad *stack_ptr = strack_quad
what about this case then? I think it does the same as number 2
closed account (zb0S216C)
bbcc wrote:
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.

Wazzak
Topic archived. No new replies allowed.