0

Can some one please explain the scope of int A and int B in the void func() and in void func1()?

class C {
  struct S {
    int A;
  };

  int B

public :
  void func(){
  }
  void func1(){
  }
};
0

1 Answer 1

1

Not sure I fully understand what you're asking for, but if I do, I'll try to answer.

Functions func() and func1() are member functions of C, so they have identical access to the exact same names here. Variable B, being a member variable of C, can be referred directly withing func() and func1() without qualifying it with any namespace.

Variable A on the other hand, being a public, non-static member variable of S, requires first instantiating an object of type S before being accessed, like this:

void func()
{
    S s;
    B = 0; // Directly accessible, member variable of `C`
    s.A = B; // A is a non-static member variable of `S`, requires an object
    A == 3; // ERROR! A is not a member variable of C
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.