Why pointers?!

May 14, 2007 at 2:22pm

Why pointers?!

Can anyone tell me why all examples are given using pointers to refer to the values? Using typed variables like int, float, etc. should be avoided?

#31908
May 14, 2007 at 2:28pm

Er, what exactly are you talking about?

jb

Am 14.05.2007 um 16:22 schrieb Justin:

>
> Can anyone tell me why all examples are given using pointers to
> refer to the values? Using typed variables like int, float, etc.
> should be avoided?

#104155
May 14, 2007 at 2:32pm

Sorry I was right in it, I didn’t realise what I said was quite ununderstandable :D
Well, I’m programming objects in C++, and all the examples are given with pointers like “x->p_value = n;”. Can’t we use simple variables?

#104156
May 14, 2007 at 2:33pm

#104157
May 14, 2007 at 2:36pm

Thank you very much, I should have foreseen it, but I would have need some distance… thank you very very much! I would have nerver found! :D

#104158
May 14, 2007 at 2:43pm

In this case, x->p_value probably refers to the p_value field of the
instance of your object. Your object is defined as a struct, is
allocated by object_alloc() or newobject() in your object’s new()
method, and is passed to the methods you’ve defined (using addmess(),
typically) as a pointer to that allocated struct. So:

typedef struct _justins_object
{
t_object ob;
long foobar;
} t_justins_object;

Every time you create a new object (a box, let’s say, containing the
name of your object), an INSTANCE of your object (this struct) is
created. A POINTER to this instance would be passed to, say, an int
method as:

justins_object_int(t_justins_object *x, long n);

x->foobar is the long integer value defined in the struct, but ONLY
for the particular instance of t_justins_object receiving the int
message.

You can, of course, define global variables in your code, and they
are available to EVERY instance of your object. But typically, you
want to only talk to one instance at a time.

jb

Am 14.05.2007 um 16:32 schrieb Justin:

>
> Sorry I was right in it, I didn’t realise what I said was quite
> ununderstandable :D
> Well, I’m programming objects in C++, and all the examples are
> given with pointers like “x->p_value = n;”. Can’t we use simple
> variables?

#104159
May 28, 2007 at 9:35pm

I think the difference is that the Max/MSP SDK is C-oriented, not C++.

You can use a C++ class in place of a C struct, and thus avoid the x-
> idiom, but the problem is that the object methods must be static.
Sometimes when using C++ for max externals I write static wrapper
functions for C++ methods, because I find the x-> idiom harder to
read, and harder to mix with other C++ styles. The wrapped functions
are inlined, so there should be no real overhead. Kind of a hack
though.

On May 14, 2007, at 7:32 AM, Justin wrote:

>
> Sorry I was right in it, I didn’t realise what I said was quite
> ununderstandable :D
> Well, I’m programming objects in C++, and all the examples are
> given with pointers like “x->p_value = n;”. Can’t we use simple
> variables?

grrr waaa
http://www.grahamwakefield.net

#104160

You must be logged in to reply to this topic.