Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9187081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:41:09+00:00 2026-06-17T19:41:09+00:00

I have a base class A and derived class B class B is derived

  • 0

I have a base class A and derived class B

class B is derived from A as public

I want to access the member variable’s address if A is class a is member variable

I am observing different behavior when i am using protected and public access specifier.

When member a of class A is protected in this case i am getting:

cout<<&A::a << endl; throwing me an compiler error..

but cout<<&(A::a)<<endl; is valid and giving me proper result.

and When member a of class A is public in this case i am getting:

Why this behavior?

Here is the full code :

#include <iostream>
using namespace std;

class A
{
    protected:
    int a;
    void fun() 
    {
    cout<<"A's fun"<<endl;
    }

public:
    A(int Aarg):a(Aarg)
    {
        cout << "A's construction done" <<endl;
    }
};


class B: public A
{
protected:
int b;
public:
void fun()
{
cout << "B's fun"<<endl;
}

B(int As, int Bs):A(As), b(Bs)
{
std::cout<<"B's Construction Done" <<std::endl;
}

void show()
{
A::fun();
//cout << a << endl;
cout<<&A::a << endl; //compilation error
}
};

int main()
{
    B(10,20).show();
    return 0;
}

Now there is a undefined behavior i am able to observe:

If i make my member variable a in class A as public then there will not be any compilation error but output is coming as 1 i dont know why..

class A{
public:
int a
....
....

OUTPUT:

A’s construction done

B’s Construction Done

A’s fun

0027F91C

1 (why 1) and no errors as I was able to get when I tried to access protected member?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:41:10+00:00Added an answer on June 17, 2026 at 7:41 pm

    Short answer: There is no undefined behavior involved. The behavior you see is:

    • The expression &A::a is an attempt to obtain a pointer to member pointing to member a of class A. If a is protected in A, this expression only passes access checks within class A (or friends of A). In a class B derived from A, you can get the same pointer to member only via the expression &B::a (note that the type of this expression will still be int A::*). So:
      • if A::a is protected in A, the expression &A::a is not allowed in a member function of derived class B. This is your compiler error.
      • if A::a is public in A, this expression is valid, producing a pointer to memeber.
    • Streaming a pointer to member to an ostream, for example using cout << &A::a will print 1. This results from invoking ostream::operator << (bool). You can use the boolalpha manipulator to see that this is indeed the chosen overload: cout << boolalpha << &A::a will print true.
    • If you use the modified expression &(A::a) or simply &a, no pointer to member is formed. Here the address of member a of the current object (i.e the same as &(this->a)) is taken, which is a regular pointer to int. This access to a protected member of a base class subobject of *this is valid, so that variant can be used even if a is protected in A.

    Longer explanation:

    The standard says (5.3.1/3):

    The result of the unary & operator is a pointer to its operand. The
    operand shall be an lvalue or a qualified- id. If the operand is a
    qualified-id naming a non-static member m of some class C with type T,
    the result has type “pointer to member of class C of type T” and is a
    prvalue designating C::m. […]

    So the expression &A::a attempts to obtain a pointer-to-member to member a of class A.

    In the next paragraph (5.3.1/4), it is elaborated that only the &X::m syntax produces a pointer to member – neither &(X::m), nor &m or plain X::m do:

    A pointer to member is only formed when an explicit & is used and its
    operand is a qualified-id not enclosed in parentheses.

    But such an expression is only valid, if access is allowed. In case of a protected member (11.4/1) applies:

    An additional access check beyond those described earlier in Clause 11
    is applied when a non-static data member or non-static member function
    is a protected member of its naming class (11.2) As described
    earlier, access to a protected member is granted because the reference
    occurs in a friend or member of some class C. If the access is to form
    a pointer to member (5.3.1), the nested-name-specifier shall denote C
    or a class derived from C. […]

    In your case access to the protected member a would be granted, because the reference to a occurs in a member of class B, derived from A. As the expression attempts to form a pointer to member, the nested-name-specifier (the part before the final “::a”) must denote B. Thus the simplest allowed form is &B::a. The form &A::ais only allowed within members or friends of class A itself.

    There is no formatted output operator for pointers to member (neither as istream member nor as free operator function), so the compiler will look at overloads that can be called using a standard conversion (sequence). The only standard conversion from pointers to member to something else is described in 4.12/1:

    A prvalue of […] pointer to member type can be converted to a
    prvalue of type bool. A […] null member pointer value is converted
    to false; any other value is converted to true. […]

    This conversion can be used without additional conversions to call basic_ostream<charT,traits>& basic_ostream<charT,traits>::operator<<(bool n). Other overloads require longer conversion sequences, so that overload is the best match.

    As &A::a takes the address of some member, it is not a null member pointer value. Thus it will convert to true, which prints as “1” (noboolalpha) or “true” (boolalpha).

    Finally, the expression &(A::a) is valid in a member of B, even if a is protected in A. by the above rules this expression does not form a pointer to member, so the special access rule quoted above does not apply. For such cases 11.4/1 continues:

    All other accesses involve a (possibly implicit) object expression
    (5.2.5). In this case, the class of the object expression shall be C
    or a class derived from C.

    Here the object impression is an implicit (*this), i.e. A::a means the same as (*this).A::a. The type of (*this) obviously is the same as the class where the access occurs (B), so the access is allowed. [Note: int x = A(42).a would not be allowed within B.]

    So &(A::a) within B::show() means the same as &(this->a) and that is a plain pointer to int.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a c++ class derived from a base class in a framework. The
I have a class which is derived from a base class, and have many
I have a class Derived that inherits directly from two base classes, Base1 and
I have a base class Element and derived two other classes from it, Solid
I have a base class and several subclasses derived from that base class. I
I have two objects that are derived from same the base class. Lets say
Suppose I have a base class and two classes derived from it: class Base
I have a base class that inherits List<> which has multiple classes derived from
Possible Duplicate: Calling virtual function of derived class from base class constructor? I have
I have two Parser classes that inherit from a base class, BaseParser. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.