Question about the code in main function tutorial 57

0 Arne Magnus Lorentzen Ulland · May 4, 2015
I thought i understood pointers and reference, but clearly i don't. 

The code:

Ninja n;
Monster m;
Enemy *enemy1 = &n;
Enemy *enemy2 = &m;
enemy1 -> attack();
enemy2 -> attack();

Its the line from 3 to 6 that confuses me, and its probably the way i think about objects and pointers.

How i think:

Enemy *enemy1 = &n;
By typing Enemy enemy1 that would make an object, but Enemy *enemy1 makes a pointer from the class Enemy called enemy1 and it gets the value equal to &n, which in other words would be something like:
pointer-enemy1 = x23faf420

enemy1 -> attack();
enemy1 is a pointer and therefore the attack-function uses the value that the pointer has to run the function. 
I think it is here i mess things up:
A function uses the reference from an object, what then? 

Post a Reply

Replies

Oldest  Newest  Rating
0 Michael Bradford · May 7, 2015
You actually understand pointers better than you think you do. So basically you have enemy1 pointing at the real Ninja object (n) in order to use the pointer to use the objects functions you would normally just use the dot operator right, and the pointer is pointing to a real object so it should be fine to say 

*enemy1.attack();


However the problem we run into is that the . operator takes precedence OVER the pointer (*) operator and assumes your trying to use the dot operator of enemy1 (the memory address not the thing at the address[*enemy1]). In order to get around this problem we use Parenthesis to explicitly tell it to use the pointer operator before the dot operator like so 

(*enemy1).attack();


This will work and compile correctly, but what the c++ people decided is that with longer function names and pointer names this becomes a very annoying and ugly process, so they created a shortcut to this syntax called the arrow operator (->) basically it removes the need to specify the pointer operator(*) and the parens, like so

enemy1->attack();


This makes it feel almost like your using a regular object when your using a pointer to an object.
Also as far as a function using a reference from a specific object, yes it does and then that object uses a reference from a pointer that we declared so it's like sort of just pointing one step further down the line.
From bottom to top: 
Usual case:
Somefunction()

Hope it helped lemme know if anything was unclear.
0 Michael Bradford · May 7, 2015
Um that lost code box should've looked like this 

Usual case:

Somefunction()
0 Michael Bradford · May 7, 2015
Ugh Here in plain text

Usual case:
somefunction() <- this (the object, in this case n)

Our case:
somefunction() <- this (the object, in this case n) <- pointer (in this case *enemy1)

I think the codeblock is actually trying to interpret my code.. my bad.
  • 1

C++

107,198 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator