Jump to content
  • Advertisement
Sign in to follow this  
Zazz

How do I return a Pointer?

This topic is 4159 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 tried looking for this, but all I found was how to point to a function. I want a function that will return a pointer (to an instance of my 'Object' class). The closest I've got is returning the address of what I want by type casting the address of the instance to an int (and having the function return an int too). And giving the pointer the address after the function is complete.
int Game::getObject(int requestedId) //Returns the address of an 'Object'
{
          /* Code to search through a list goes here */

	if (requestedId == temp->getID())
	      { return (int)&temp; } //Found
	return NULL; //If not found
} 

//Assigning the address to an 'Object' pointer
Object *objectPtr = (Object*)game.getObject(1);

But the problem with this is that I get a warning saying that an Object is bigger than an int. I don't know if this is a warning I can ignore because I don't know enough about memory yet (i.e. If an int can hold the address to an instance, or if it need to be big enough to store every element of the class). So I would appreciate it if anyone could tell me how to return a pointer. If not, how can I give the returned address to an Object pointer? Cheers

Share this post


Link to post
Share on other sites
Advertisement

Object* returnObjPtr()
{
Object *o = new Object();
return o;
}


Be careful not to return a pointer to an object on the stack; it will get destroyed when the function terminates, invalidating the pointer.

Share this post


Link to post
Share on other sites
Is there any reason you aren't doing the obvious? Declare the return type to be Object * instead of int.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zazz
But the problem with this is that I get a warning saying that an Object is bigger than an int. I don't know if this is a warning I can ignore because I don't know enough about memory yet (i.e. If an int can hold the address to an instance, or if it need to be big enough to store every element of the class).
Ints are usually 4 bytes, pointers are (always) 4 bytes when compiling code for a 32-bit system. However, on a 64-bit system, that's not nessecarily true. For instance, compiling code for x64 with visual studio, pointers are 8 bytes, and ints are still 4, meaning casting a pointer to an int will result in half the data going "missing".

Share this post


Link to post
Share on other sites
If you do not want to declare return type to any specific pointer, like Object*, you can declare it as void* and typecast the return value to your specific type, just like you do in your code snippet with the int*. But I see no purpose to do this on most situations as declaring everything to specific pointer types makes usually more sense.

Share this post


Link to post
Share on other sites
Quote:
Original post by pieslice
If you do not want to declare return type to any specific pointer, like Object*, you can declare it as void* and typecast the return value to your specific type, just like you do in your code snippet with the int*.


This is almost always a bad idea. Throwing around void*'s and casting them to the types you *expect* them to be can lead to serious trouble. In C++, you can take advantage of templates in most cases where you think a void* may be called for.

Share this post


Link to post
Share on other sites
Driv3MeFar:
Cheers, what was the answer I was looking for.

SiCrane:
That was the first thing I tried. It always gave me errors...
Turned out I was doing *Object insted of Object* >_<
(I guess "int* x" and "int *x" meaning the same thing must have confused me)

Evil Steve:
Thanks, that's good to know.

Cheers everyone.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zazz
I guess "int* x" and "int *x" meaning the same thing must have confused me
Think of it as just the position of the asterisk, since C++ doesn't care about whitespace. "int*x" is also valid, as is
"int            *            x"

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!