Jump to content
  • Advertisement
Sign in to follow this  
DOrmisher

Passing pointers as function parameters

This topic is 3511 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 a got a function:
void CMultiset< Type >::AllocateMemory(int Size, CObject<Type> *front, CObject<Type> *back )
{
	//Create the first object, make it the front AND back
	CObject< Type >* obj = new CObject< Type >(NULL, NULL, 0);
	front = back = obj;

	CObject< Type >* obj2;

	//Allocate memory for the size indicated
	for(int i=1; i < Size; i++)
	{
		obj2 = obj;
		obj = new CObject< Type >( obj2, NULL, i );
		obj2->m_Left = obj;

		front = obj;
	}
}
The function works fine in itself, the front and back pointer are allocated correctly, but for some reason it is not updating the pointers I have passed to the function. I'm sure it'll be simple but can someone help please?

Share this post


Link to post
Share on other sites
Advertisement
The pointers themselves are passed by value not by reference, so you're only modyfing the local copies of the pointers. You probably want something like void CMultiset< Type >::AllocateMemory(int Size, CObject<Type> *&front, CObject<Type> *&back )

Share this post


Link to post
Share on other sites

void A(int i)
{
i = 3;
}

int foo = 2;
A(foo); // foo is still 2... why?

void B(float* f)
{
*f = 3.0f;
}

float bar = 2.0f;
B(&bar); // now bar is 3.0... why?

void C(float* p)
{
p = new float;
}

float zzz;
float* baz = &zzz;
C(baz); // baz still points to zzz... why?

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!