views:

218

answers:

5

Hi, still working at C++, but this came up in my book and I don't understand what it's for:

MyClass * FunctionTwo (MyClass *testClass) {
    return 0;
}

My question is what is the signifigance of the first indirection operator

(MyClass *[<- this one] FunctionTwo(MyClass *testClass))?

I tried making a function like it in codeblocks, with and without the first * and I didn't see any difference in the way it ran or it's output:

int *testFunc(int *in) {
    cout << in << endl;
    cout << *in << endl;
    return 0;
}

int testFuncTwo(int *in) {
    cout << in << endl;
    cout << *in << endl;
    return 0;
}

I couldn't find anywhere that explained about it in my book.

Thanks,

Karl

+10  A: 

The MyClass * means that this function returns a pointer to a MyClass instance.

Since the code is actually returning 0 (or NULL) this means that any caller to this function gets back a NULL pointer to a MyClass object.

I think the following are better examples:

int *testFunc(int *in) {
    cout << "This is the pointer to in " <<  in << endl;
    cout << "This is the value of in "  << *in << endl;
    return in;
}

int testFuncTwo(int *in) {
    cout << "This is the pointer to in " <<  in << endl;
    cout << "This is the value of in "  << *in << endl;
    return *in;
}


void test() {
  int a = 1;

  cout << "a = " << a;

  int output = *testFunc(&a); // pass in the address of a
  cout << "testFunc(a) returned " << output;

  output = testFuncTwo(&a); // pass in the address of a
  cout << "testFuncTwo(a) returned " << output;

}

Apologies, but I've not done C++ in years but the syntax may be a little off.

Preet Sangha
+1  A: 

When you define:

int *testFunc(int *in)

You are defining a function which returns a pointer to an int variable.

int testFuncTwo(int *in)

Returns just an int variable.

Zero in this case is undergoing an implicit cast - the first function returns (int*) 0, the second just 0. You can see this implicit cast in action if you change the prototype of your example function from returning a MyClass* to just returning a MyClass - if there's no operator int method in MyClass, you'll get a nice error.

Matthew Iselin
Ah ok, that makes sense. Thanks m8.
Karl Menke
+1  A: 

If you had something like int * Function() this would return a pointer to an integer. MyClass * just means that the function is going to return a pointer to an object of type MyClass. In c++ user defined objects are treated as first-class-objects, so once you create your own objects they can be passed as a parameter, returned from a subroutine, or assigned into a variable just like the standard types.

In the code blocks you posted there will be no difference in the output to the console because the thing you have changed is the type that the function returns (which is not used), the contents of the functions are identical.

shuttle87
+2  A: 

Given the nature of the question, I'm going to provide a somewhat crude answer.

A pointer points to something:

int x = 123; // x is a memory location (lvalue)
int* y = &x; // y points to x
int** z = &y; // z points to y

In the above code, z points to y which points to x which stores an integral, 123.

x->y->z[123] (this is not code, it's a text diagram)

We can make y point to another integer if we want or NULL to make it point to nothing, and we can make z point to another pointer to an integer if we want or NULL to make it point to nothing.

So why do we need pointers which point to other things? Here's an example: let's say you have a simple game engine and you want to store a list of players in the game. Perhaps at some point in the game, a player can die by having a kill function called on that player:

void kill(Player* p);

We want to pass a pointer to the player, because we want to kill the original player. Had we done this instead:

void kill(Player p);

We would not kill the original player, but a copy of him. That wouldn't do anything to the original player.

Pointers can be assigned/initialized with a NULL value (either NULL or 0) which means that the pointer will not point to anything (or cease to point to anything if it was pointing to something before).

Later you will learn about references which are similar to pointers except a pointer can change what it points to during its lifetime. A reference cannot, and avoids the need to explicitly dereference the pointer to access the pointee (what it's pointing to).

Note: I kind of skirted around your original question, but the sample code you provided has no inherent meaningful behavior. To try to understand that example without first understanding pointers in a general sense is working backwards IMHO, and you'd be better to learn this general theory first.

+2  A: 

The general format for a function that takes a single value and returns a single value in C++ is:

return_type function name (    parameter_type parameter_name )

In your case, return_type is MyClass *, which means "pointer to MyClass" In addition, in your case, parameter_type is also MyClass *.

In other words, your code could be rewritten as:

typedef MyClass *PointerToMyClass;
PointerToMyClass FunctionTwo (PointerToMyClass testClass)
{
 return 0;
}

Does that look more familiar?

carleeto