How do I return a Pointer?

Started by
7 comments, last by Evil Steve 16 years, 9 months ago
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
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.
Is there any reason you aren't doing the obvious? Declare the return type to be Object * instead of int.
int* GetObject(int requestedId){    //...    return &temp}
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".
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.
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.
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.
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"

This topic is closed to new replies.

Advertisement