Skip to content

Commit 80df28f

Browse files
committed
.
1 parent bb984fa commit 80df28f

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

58-PointerToMember.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//58-PointerToMember
2+
3+
#include <iostream>
4+
5+
class A{
6+
public:
7+
void f(int x){
8+
std::cout << "A::f is called. " << x << std::endl;
9+
}
10+
11+
char x;
12+
};
13+
14+
int main(){
15+
using pf = void (A::*)(int);
16+
pf sf = &A::f; //pointer to member function.
17+
18+
using pd = char (A::*);
19+
pd sd = &A::x; //pointer to member data.
20+
21+
A *a = new A();
22+
a->f(1);
23+
(a->*sf)(2);
24+
25+
a->*sd = 'a';
26+
std::cout << a->*sd << std::endl;
27+
}

MODERNCPP_REMINDER.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* Constructor: builds objects from predefined types. explicit purpose is initializing objects.
1919
* Container: generic collection of class templates and algorithms that allow programmers to easily implement common data structures.
2020
* Contextual Keyword: has special meaning in few contexts but can be used as ordinary identifier elsewhere. example: final, override.
21+
* Contravariance: assigning pointer to member of base class to pointer to member of derived class.
2122
* Copy Constructor: makes copy of object without modifying it.
2223
* Declaration: introduce names into program.
2324
* Default Constructor: constructor that can be called with no arguments, or that can take arguments, provided they are given default values.

0 commit comments

Comments
 (0)