jeudi 21 octobre 2021

Accessing global variable with this-> pointer in C++? [duplicate]

#include <stdio.h>
void val();
int v=10;

class test{
public:
    int v=11;
    void val()
    {
        printv();
    }
private:
    void printv()
    {
        int v=12;
        printf("V: %d",this->v);
    }
};

int main()
{
    test obj;
    obj.val();
    return 0;
}

I am getting 11 as output but I need to access the global variable, How can I get it ?? Similarly are there any ways to access global variables with the same name as local variables in C & Python ??

Aucun commentaire:

Enregistrer un commentaire