samedi 10 octobre 2020

What happened to arguments in CPP constructors? Why this->variable is now same as argument variable in C++ constructors? [closed]

I have recently installed Ubuntu 20.04 LTS WLS on windows 10 PC

And inside Ubuntu I installed gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)

Now I made this simple C++ code

#include <iostream>

using namespace std;

class Firepower
{
private:
    uint64_t damage_power = 100;
public:

    Firepower(uint64_t damgage_power)
    {
        this->damage_power = damage_power;
        cout << "damage_power:"       << damage_power        << endl; //why is it not 50?
        cout << "this->damage_power:" << this->damage_power  <<endl;
    }

    void print_power(){
        cout << (this->damage_power) << endl;
    }
};


int main(int argc, char const *argv[])
{
    Firepower fp(50);
    fp.print_power();

    return 0;
}

I was expecting the output to be, with Knowledge I had:-

damage_power:50
this->damage_power:50
50

But Real output was

damage_power:100
this->damage_power:100
100

Is it something about c++ 11? Or I just rippled into a different universe?

I mean what I learned few years back in my college was that when we have a class Lets say:-

class C{
   int n;
   public:
   C(int n){
      this->n ;//is the value of the variable n in object of class C
      n       ;//is the value of the variable n passed in the argument
   }
}

I am also remember that we were made to do the same thing on our practical sessions too.

SO I am confused why this the case?

Aucun commentaire:

Enregistrer un commentaire