-10

Sorry, this is a very noob question. I'm a fresh c++ student...

I am trying to understand a c++ code I found online for a space shooter like game. I want to use it for my project. The hero shoots arrows to an enemy. It has this &ss, ss.X and !ss but I don't know what they mean. What does the . between variables mean? And what does &ss mean? Would you please share any topic or lesson related to this? Or do you know what term these are called? I'm trying to search it on google, but I don't know the right keyword.

This is when the monster hits the hero. This is a function inside a class of my monster.

class monster
{
private:
int x;
int y;
public:
int X()  { return x; } 
int Y()  { return y; }

void Collision(Hero &ss) 
   {
   if(((x >= ss.X()) && (x <= ss.X() + 5)) && ((y >= ss.Y()) && (y <= ss.Y() + 2)))
   { 
   ss.Damage(); //a function under my hero class which will decrease hero energy
   gotoxy(x,y); printf(" "); 
   x = rand()%74 + 3; 
   y = 4;
   }
}

Thank you!

  • 6
    Any basic C++ tutorial will cover all of this for you. – John3136 Oct 26 '18 at 06:59
  • All of those are *operators*. Look up an operator list for C++... – HolyBlackCat Oct 26 '18 at 07:01
  • 1
    you shouldn't post a question without knowing anything about C++. would be better for you to check through some tutorials for beginners. – duong_dajgja Oct 26 '18 at 07:03
  • 2
    [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Oct 26 '18 at 07:10
  • If you really expect to understand these symbols by just reading on stackoverflow, you'd be deluded! I can tell you, the first one is a reference to `s`, the second is the NOT operator on `s`, and the third one is accessing a member of `s`. Did that teach you anything? With no background, it's pointless. You have to work hard to understand C++. Coding is hard and needs hard work. Pick a book and start reading! – The Quantum Physicist Oct 26 '18 at 08:20

2 Answers2

2

I think you need to start to c++ basic tutorials. Take a look to this page: https://www.learncpp.com

&ss means that your object of type Hero is passed as reference. Understanding you're a beginner in C++, you don't know what means pass by reference. So let's see an example:

If you declare your function as void Collision(Hero ss) { } and then use it in your main() { }.

void Collision(Hero ss) {}

int main() 
{
  Hero MyHero;
  Collision(MyHero);
}

This means that inside your Collision method ss parameter will be a copy of your MyHero object. So any modification you will do to ss, MyHero will not be modified.

But if you declare void Collision(Hero &ss) {}:

void Collision(Hero &ss) {}

int main() 
{
  Hero MyHero;
  Collision(MyHero);
}

This means ss will be Myhero object. This implies any modification you will do to ss will modify MyHero object.

  • 1
    I see. Thank you very much for the examples and explanation. This is the part that I'm still confused about in my class. I just started c++ so excuse my noobness. So these (&ss) are just parameters, not special keywords. – DigitalArtistB Oct 26 '18 at 07:39
1

Hero& ss is passing an object of the class Hero by reference (pass by reference is the term to search for). This means more or less that the adress of the object is passed into this function, and since it's not a const reference, you can call all methods of the class (const and non-const).

ss.X is actually ss.X() and is a call of the method X() in Hero, which returns the value of the member variable x.

I don't see any !ss in your example, but that would call an operator in Hero that returns a boolean value, and the result would be true if the returned value is false (! being the not operator).

Rene
  • 2,466
  • 1
  • 12
  • 18