I do not understand this tutorial about pointers

    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 I don't understand why you need *

FunctionTwo? If you really want to do me a favor, someone can break the code for me (pointer part, because I seriously don't understand when and why to use *

and &

.

+3


source to share


3 answers


FunctionTwo returns a pointer to a SimpleCat object. As you can see there, it also takes a pointer to a SimpleCat object as a parameter. It just takes a pointer and then returns it in this case.

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



However, when a similar statement is written to the parameter list of a function, i.e. SomeFunction (SimpleCat & Frisky), what it tells you is that objects are passed to the function by reference. This basically lets you take advantage of one of the advantages of pointers without worrying about the syntax of the pointer. You would normally call a function using SomeFunction (Frisky), and inside the function, you would use Frisky with the same syntax as in the main function, but you must remember that the object is not copied. Both inside the main one and inside SomeFunction, you perform operations on the same object. It is not copied. This feature is provided only with the information needed to access Frisky.

+1


source


Ok. It's pretty easy. A pointer is an operation that refers to some space in memory. So say that you have allocated space for an instance of your class in memory, and a pointer allows you to refer to the starting point of that space. As far as you know, this is the definition of an instance class residing in this chunk of memory, you can use all the methods and fields of this class. The link below can help you deal with this What is the difference between a pointer variable and a reference variable in C ++? And yes, your function simply returns a SimpleCat *, which is a pointer to a SimpleCat structure.



0


source


OK lets you start with the basics:

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

      

The only thing here might not make sense if FuctionTwo(&Frisky)

. What it does is pass the address Frisky

to FucntionTwo()

. When &

placed in front of a variable of any type, it simply means grabbing the address of that variable.

Now let's look at FunctionTwo()

:

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

      

FunctionTwo

takes a pointer SimpleCat

. In this case, it is SimpleCat

already defined as the structure of the class you defined above. What it means *

is that whenever you work with a variable theCat

, you are working with the address of what was passed into the function. This is why you need to dereference Frisky

before passing it to a function. The function looks for the address of what is being passed.

Finally, you see what the FunctionTwo

variable is returning theCat

. Since it was passed as a Pointer , it must be returned as a Pointer. This is why the function is abbreviated as SimpleCat *FunctionTwo()

, it prepares the code for an event where the return type is not an integer value, but a reference to a variable with a value.

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

0


source







All Articles