Sign in to follow this  
jagguy2

passing DX pointers

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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, syntactically
assert(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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
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);


i did try this and got an error



in the .h file for the class i have
HRESULT call( LPD3DXMESH ,WORD*);


HRESULT Mya::call( LPD3DXMESH g_mesh,WORD *&i) //ERROR //overloaded member function not found in 'call''
{
g_mesh->LockIndexBuffer(0, (void**)&i);
}

//and i call the function above from a main file like
WORD *i;
mya.call(g_mesh, i);

Share this post


Link to post
Share on other sites
Here is a complete example of a similar problem when i pass a pointer to be changed.

How do i fix this so i can pass a pointer and change it?

'void door::start(WORD *&)' : overloaded member function not found in 'door'




class door{
public:
WORD a;

void start(WORD*);
};

void door::start(WORD *&i){
//cout <<"hello"<<endl;
a=3;
*i= new WORD
*i=8;
cout << *i <<endl;
}




int main(int argc, char *argv[])
{


//door *d = new door;
//door *dd=new door[10];
door b;
WORD *i;

b.start(i);

cout << b.a <<endl;


system("pause");
return 0;



}

Share this post


Link to post
Share on other sites
The function's signature must be identical in both the .h file and .cpp file. In your case, you only changed the signature in the .cpp file (by adding a '&'). To fix this, add it in the .h file in the same place as well.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this