3
    class SimpleCat
{
public:
    SimpleCat();
    SimpleCat(SimpleCat&);
    ~SimpleCat();
};

SimpleCat::SimpleCat()
{
    cout << "Simple Cat Constructor.. \n";
}

SimpleCat::SimpleCat(SimpleCat&)
{
    cout << "Simple Cat Copy Constructor ..\n";
}

SimpleCat::~SimpleCat()
{
    cout << "Simple Cat Destructor! ... \n";
}

SimpleCat *FunctionTwo(SimpleCat *theCat);

void main()
{
    cout << "Making a cat ...\n";
    SimpleCat Frisky;
    cout << "Calling FunctionTwo ..\n";
    FunctionTwo(&Frisky);
    system("pause");
}

SimpleCat *FunctionTwo (SimpleCat *theCat)
{
    cout << "FunctionTwo, Returning... \n";
    return theCat;
}

Ok, so what i don't understand is, why do you need the * for FunctionTwo? If you really want to do me a favor, can someone please break down the code for me (the pointer part, because i seriously don't understand when and why to use the * and &.

Manlio
  • 10,768
  • 9
  • 50
  • 79

3 Answers3

1

FunctionTwo returns a pointer to a SimpleCat object. As you can see there, it also accepts a pointer to a SimpleCat object as a parameter. It's just accepting the pointer and then returning it in this case.

To call that function, you need to pass a pointer to it. If you want to pass Frisky to the function, you need to pass the address of the object Frisky. This is what is being done when &Frisky is written. A pointer is created with the address of the Frisky object.

However, when a similar statement is written in the parameter list of a function, i.e. SomeFunction(SimpleCat& Frisky) what it is telling you is that objects are passed to the function by reference. This basically allows you to use one of the advantages of pointers without worrying about pointer syntax. You would call the function normally by saying SomeFunction(Frisky), and within the function you would use Frisky with the same syntax as you would within the main function, but you should remember that the object is not copied. Both within main and within SomeFunction, you are performing operations on the exact same object. It is not copied. Only the information required to access Frisky is given to the function.

  • Thank you very much, makes complete sense now :D – Lasha Zakariashvili Mar 25 '12 at 00:56
  • There are many subtleties to the use of pointers and references in C++. I suggest you do some extra reading to make sure you understand all the different cases and why you should use one method instead of another in a certain situation. Good luck. – ShiggityShiggityShwa Mar 25 '12 at 00:58
0

Ok. This is pretty easy. A pointer is an operation, that refers to some space in memory. So say you've allocated some place for an instance of your class in memory, and a pointer allows you to refer to the starting point of this space. As far as you know exactly is the class definition of the instance located in this chunk of memory you can use all the methods and fields of the class. The link below can help you to deal with it What are the differences between a pointer variable and a reference variable in C++? And yes, your function just returns a value of type SimpleCat* which is a pointer to SimpleCat structure.

Community
  • 1
  • 1
amdmax
  • 771
  • 3
  • 14
0

OK lets start by looking at the main:

void main()
{
    cout << "Making a cat ...\n";
    SimpleCat Frisky;
    cout << "Calling FunctionTwo ..\n";
    FunctionTwo(&Frisky);
    system("pause");
}

The only thing here that may not make any sense to you if FuctionTwo(&Frisky). What this is doing is passing the address of Frisky to FucntionTwo(). When & is placed in front of a variable of any kind it simply means grab the address of this variable.

Now lets look at FunctionTwo():

SimpleCat *FunctionTwo (SimpleCat *theCat)
{
    cout << "FunctionTwo, Returning... \n";
    return theCat;
}

FunctionTwo takes an pointer of SimpleCat. In this case SimpleCat is already defined as the structure of the class you have defined above. What the * means is that whenever you are working with the variable theCat you are working with the address of whatever was passed into the function. This is why you need to dereference Frisky before passing it into the function. The function is looking for the address of whatever is being passed.

Last you see FunctionTwo is returning the variable theCat. Since it was passed in as a Pointer it needs to be returned as a pointer. That is why the function is being decleared as SimpleCat *FunctionTwo(), this is preparing the code for an event where the return type isn't a whole value, but a reference to a variable with the value.

This is where you get the concepts of Pass by Reference and Pass by Value.

Blackninja543
  • 3,639
  • 5
  • 23
  • 32