passing DX pointers

Started by
11 comments, last by Gage64 16 years, 3 months ago
I cant pass directx pointers to member functions as i keep getting access violation. So what i want is just and example of how to do this. ------ LPD3DXMESH g_mesh=0; mya.call(g_mesh, i)) //this compiles --------- --------- in the class i use HRESULT Mya::call( LPD3DXMESH g_mesh,WORD *i) g_mesh->LockIndexBuffer(0, (void**)&i); //this compiles but it doesnt work ------ if i use g_mesh again out side the class it fails with access violation how do I pass data types to member classes like LPD3DXMESH and use them again out side the class?
Advertisement
I assume your code looks like this:

HRESULT Mya::call( LPD3DXMESH g_mesh,WORD *i){    g_mesh->LockIndexBuffer(0, (void**)&i);}


that is, that LockIndexBuffer() is called inside of call(). If not please correct me (the way you wrote it isn't very clear).

I'll try to explain why this doesn't work. When you write:

WORD *i;mya.call(g_mesh, i);


then a copy of the pointer i is passed to call(). Anything you do to this copy will not affect the original pointer (however you can change what the pointer points to from inside call(), because both the external i and the parameter i point at the same thing).

If you want to change the actual pointer (and not what it points to), you have to pass a pointer/reference to the pointer. So one way to fix this is:

HRESULT Mya::call( LPD3DXMESH g_mesh,WORD *&i)  // Notice the '&'{    g_mesh->LockIndexBuffer(0, (void**)&i);}


Note that because you are using a reference, the code that calls call() doesn't change, that is you still write:

WORD *i;mya.call(g_mesh, i);
hi,

*&i ? i never saw this before as i thought passing any pointer means you can
change it.

q)this is the same for passing any pointer like g_mesh which really confused me when passing to a member function.

q) I have a problem with assigning a pointer and initial value to

WORD *i=0; //in a class i cant do this but in a function i can.
I tried to just declare

WORD *i;
in a class header file and add *i=0 in the constructor but this fails as i get an error.

So what do i do?

[Edited by - jagguy2 on January 3, 2008 4:07:19 AM]
Quote:*&i ? i never saw this before as i thought passing any pointer means you can
change it.


Change what? The object that the pointer points to - yes. The pointer itself - no. Like I said, the pointer is passed by value (i.e, a copy of it is passed to the function), so anything done to this copy will not affect the original pointer. To change the pointer itself, you need to pass a pointer/reference to the pointer.
so what does changing the pointer mean and how does this change the pointer 'i'?


g_mesh->LockIndexBuffer(0, (void**)&i);
Changes what pointer? g_mesh - no. i - yes.
to use g_mesh created from a function in main i cant pass it to a class function to be used to allocate data like this g_mesh->LockIndexBuffer(0, (void**)&i);

that was my original problem. To use g_mesh in such a way what do i do?
I'm not sure what you mean. Can you post more code and explain exactly what's wrong with it?
Quote:Original post by jagguy2
so what does changing the pointer mean and how does this change the pointer 'i'?


g_mesh->LockIndexBuffer(0, (void**)&i);


You have two things when dealing with pointers. A value (which we'll represent using a sign) and a pointer (which we'll represent using an arrow on a stick).

The arrow points to the value, so we can 'dereference' the pointer (by looking at the arrow to see where it's pointing, and walking over to the sign it's pointing at).

By dereferencing the pointer, we can change the value it points to — writing a different number on the sign, for example.

However, we can also change the pointer itself (called 'reseating'). You can imagine this as changing where the arrow is pointing so that it points to another box. The previous box still exists, but we don't know where it is any more (if no one knows where it is any more, then we might have a memory leak — assuming it was dynamically allocated (created using new or new[]).

--

Now, when we pass a pointer into a function, we're creating a copy of the pointer (we look at the arrow to see where it's pointing, cut out a new arrow and point it in the same direction, and then pass that to our function).

If the function changes the pointer, it only changes its copy — our arrow stays pointing the same direction as always, even if the function's arrow is pointing at a completely different sign from the one it did intially. However, if the function dereferences the pointer, it can change the object that it points to — this object could be the same as our object (if both arrows are pointing towards the same sign).

--

Now, sometimes we want the function to change our pointer. To do this, we pass a pointer to our pointer (an arrow pointing at our arrow!) to the function. Now, the function can change its pointer (making its arrow point to some other arrow) or dereference it and change ours (looking at its arrow, following it and changing our arrow).

--

(End of analogy)

One problem with this analogy is that there are a couple of special types of pointers which don't point at anything in particular. NULL pointers (evaluate to 'false' if cast to bool; 0 will cast to NULL for any type T* — both casts can occur implicitly) point to nothing and are used to signify failures, successes, or that some pointer is to be ignored or set to a default value. A one-past-the-end pointer is used to denote the end of some range (as in the standard library, where end iterators are one-past-the-end). It's not really relevant what this points to (it may point to nothing, or to something), other than to note that, unlike NULL, it doesn't have some particularly special value to denote it.

A caveat to all of the above is that C++ also provides references (C++ FAQ Lite). These are conceptually similar to pointers which can only ever point to one thing (the pointer cannot be changed —). Now, this might seem like syntactic sugar, since after all we can have pointers of type T* const (const pointer — we can dereference the pointer and change the object it points to be, but we can't change the pointer so that it points to a different pointer). Now, I suppose it is, but it runs deeper — a reference to an object, for all intents and purposes, is that object under a different name.

That is,

int x = 5;       // An int called 'x'int& y = x;      // A reference-to-int called 'y'assert(&x = &y); // Taking the address of a reference (y) gives the address of the referent (x)x = 6;assert(y == 6);y = 7;           // No such thing as 'dereferencing' a reference, syntacticallyassert(x == 7);  // We can use either the reference 'y' or the original 'x' to                 // change the same object -- because they're both the same                  // thing


Ignore the fact that the syntax for declaring a reference is somewhat similar to taking the address of an object.

Where possible (when you don't need to reseat the pointer), prefer references. Apart from anything else, you don't have to worry about dereferencing them or any other similar syntactic noise.

I hope that helps (and that I haven't made any mistakes... I've edited a couple times to try to be as informative and accurate as possible).

[Edited by - TheUnbeliever on January 3, 2008 7:44:44 AM]
[TheUnbeliever]
i will goto bed now and try my code in the morning as i think i see the problem.

I was assigning g_mesh->lock....&i) where 'i' was local variable in a class function and g_mesh in passed to the class from a regular function. I was assigning g_mesh a local pointer variable which will fail when i exit the class.

This topic is closed to new replies.

Advertisement