Jump to content
  • Advertisement
Sign in to follow this  
freeworld

how do you pass a pointer as a refrence or is that what I want

This topic is 3621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have objects outside of a function that I want it to be able to set, so I wanted to pass refrences of them to the function so the function can edit them? or is that what I'm trying to get at.

Object *outside_object;
int     outside_variable;

Foo_Function(Object& obj, int& var)
{
    obj = inside_object;
    var = &inside_var;
};


is that right?

Share this post


Link to post
Share on other sites
Advertisement
Not quite sure what you mean. If you want to pass objects to the function to manipulate, yeah you can pass a reference to them.


void foo( Object& a )
{
a.x = 1.f;
}


...
Object outsideObject;
Object* otherObject;

void main()
{
foo( outsideObject );
foo( *otherObject );
}


Share this post


Link to post
Share on other sites
You want to modify the pointer itself? Like this?

void change(object*& ptr)
{
ptr = otherObject;
}

object *ptr = thisObject;
// ptr == thisObject
change(ptr);
// ptr == otherObject

Share this post


Link to post
Share on other sites
I guess what i wanted to say was.

I want to pass a pointer to a function and change what it points to in the function, I'd also like to pass a variable to the same function and change that variable value.

in the declaration of my function looks similar to this and I'm getting one error.


MyFunction(ptrObject& ptrobj=NULL, int& var=0)



but I'm getting a c2440 error - cant convert default argument from 'int' to 'int&'

[Edited by - freeworld on July 21, 2008 9:16:35 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by freeworld
I guess what i wanted to say was.

I want to pass a pointer to a function and change what it points to in the function, I'd also like to pass a variable to the same function and change that variable value.


void Foo_Function(Object*& pointerRef, int& intRef)
{
int inside_int = 0;
Object* inside_pointer = 0;

pointerRef = inside_pointer;
intRef = &inside_int;
};

Object *outside_pointer = 0;
int outside_int = 0;
Foo_Function( outside_pointer, outside_int );

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!