Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you explain this difference in pointer to members of base and derived class using standard quotes?

demo:

#include<iostream>
struct A { int i = 10; };
struct B : A { };

int main(){
    std::cout << "decltype(&B::i) == int A::* ? " << std::boolalpha
              << std::is_same<decltype(&B::i), int A::*>::value << '\n';    //#1
    A a;
    std::cout << a.*(&A::i) << '\n';

    std::cout << "decltype(&B::i) == int B::* ? "
              << std::is_same<decltype(&B::i), int B::*>::value << '\n';    //#2
    B b;
    std::cout << b.*(&B::i) << '\n';
}

The code prints

decltype(&B::i) == int A::* ? true
10
decltype(&B::i) == int B::* ? false
10

I used the example in [expr.unary.op]/3, where the standard says that the type of &B::i is int A::*, but that is not normative.

like image 770
Alexander Avatar asked Jun 16 '19 13:06

Alexander


People also ask

What is the difference between a base class pointer and a derived class pointer?

Explanation: A base class pointer can point to a derived class object, but we can only access base class member or virtual functions using the base class pointer because object slicing happens when a derived class object is assigned to a base class object.

How the base class reference can point to derived class object what are its advantages?

So, a pointer is type of base class, and it can access all, public function and variables of base class since pointer is of base class, this is known as binding pointer. In this pointer base class is owned by base class but points to derived class object. Same works with derived class pointer, values is changed.

Can a derived class pointer point to a base class object?

Derived class pointer cannot point to base class.

What is the function of base and derived class?

The Base class members and member functions are inherited to Object of the derived class. A base class is also called parent class or superclass. Derived Class: A class that is created from an existing class. The derived class inherits all members and member functions of a base class.


1 Answers

From the paragraph you link to, emphasis mine:

If the operand is a qualified-id naming a non-static or variant 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.

"Some class C" means it need not be the same class as the one mentioned by the qualified-id. In this case, i is a member of A, and remains a member of A even when named by &B::i. The type of &B::i is therefore int A::*, which you can verify by the test

std::is_same<decltype(&B::i), int A::*>::value

According to [class.qual]/1, member lookup follows the algorithm detailed in [class.member.lookup]. It is according to the rules there, which inspect the sub-object from which the member i comes from, that the class C is determined. Since i is a member of the sub-object A, the class of the pointer to member is determined to be A.

like image 137
StoryTeller - Unslander Monica Avatar answered Sep 19 '22 05:09

StoryTeller - Unslander Monica