0
#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 ??

Meganathan
  • 25
  • 6
  • 1
    Use `::v`. Better, don't write such code in the first place. – Nate Eldredge Oct 22 '21 at 03:29
  • *Similarly are there any ways to access global variables with the same name as local variables in C & Python* -- Advice -- pretend that C and Python do not exist. Do not use other languages as a model in writing proper C++ code. – PaulMcKenzie Oct 22 '21 at 03:31
  • C and Python are two separate questions that would belong in separate posts. However, search carefully first as they have almost surely been asked and answered before. For C the only way I know is to declare `extern int v;` inside a nested block. – Nate Eldredge Oct 22 '21 at 03:34
  • @NateEldredge Thanks Nate . Yeah sure it's just for testing purpose, anyways thanks a lot Nate. – Meganathan Oct 22 '21 at 03:38

0 Answers0