类成员指针
类成员指针是指可以指向类的非静态成员的指针。成员指针指向的是类的成员。类的静态成员不属于任何对象,因此无须特殊的静态成员的指针,指向静态成员的指针和普通函数指针没有什么区别。当初始化一个类成员指针时,令其指向类的某个成员,但是不指定该成员所属的对象,直到使用成员指针时,才提供成员所属的对象。
数据成员指针
类成员指针的使用*表示当前声明的名字是一个指针,但是在*之前必须添加className::表示当前定义的指针可以指向className的成员。例如:
const std::string Screen::*pData;//pdata可以指向一个常量(非常量)Screen对象的std::string成员
上述语句将pData声明成“一个指向Screen类的const std::string 成员的指针”。const对象的数据成员本身也是const的,pData声明为const std::string意味着可以指向任何Screen对象的一个成员,不管该Screen对象是否是cosnt的。但是使用pData只能读取它所指的成员,而不能向它写入数据。
初始化类成员指针:
pData = &Screen::contents;//取地址符&作用于Screen类的成员而非内存中的一个该类的对象。
用C++11语法声明类成员指针最简单的方法是使用auto或decltype:
auto pData = &Screen::contents;
类数据成员指针
例1:
#include <iostream>
#include <string>
class Person
{
public:
const std::string name="1234";
};
int main(void)
{
Person person1;
Person* person2 = new Person();
//str为指向Person对象的const string成员的指针
//str指定了成员name,此时没有指向任何数据
const std::string Person::*str = &Person::name;
//使用对象实例解引用成员指针
std::cout<< person1.*str <<std::endl;
std::cout << person2->*str << std::endl;
return 0;
}
运行结果如下:

例2:
#if 1
#include <iostream>
#include <memory>
#include <string>
class Person
{
public:
const std::shared_ptr<std::string> name{std::make_shared<std::string>("1234")};
};
int main(void)
{
Person person1;
auto person2 = std::make_shared<Person>();
//str为指向Person对象的const string成员的指针
//str指定了成员name,此时没有指向任何数据
const std::shared_ptr<std::string> Person::*str = &Person::name;
//使用对象实例解引用成员指针
std::cout<< *(person1.*str) <<std::endl;
return 0;
}
运行结果如下:

例3:
#include<iostream>
#include <string>
class Person
{
public:
const std::string name="1234";
bool testFunc(int height)
{
std::cout << height << std::endl;
return true;
};
};
int main(void)
{
bool (Person::*func)(int)=&Person::testFunc;
Person person1;
(person1.*func)(10);
Person* person2 = new Person();
(person2->*func)(20);
system("pause");
return 0;
}
运行结果如下:

using ValueType = unsigned int;
class CppGame
{
public:
CppGame():name{"CppChallengeGames"}, player{99}{}
static ValueType getNumOfSg() {return numOfSg;}
ValueType getPlayer() const {return player;};
ValueType getPlayer() {return player;};
static std::string const CppGame::*data(){return &CppGame::name;}//静态成员,返回一个成员指针(成员类型为std::string)
void show(){std::cout << "rank:" << std::endl;}
private:
std::string name{};
ValueType player;
static constexpr ValueType numOfSg{10};
};
CppGame myCppGame, *pCppGame = &myCppGame;
const CppGame cppGame;
using Action = ValueType (CppGame::*)() const;
Action pmfc = &CppGame::getPlayer;//指向的是const的getPlayer成员函数
ValueType (CppGame::*pmf)() = &CppGame::getPlayer;//non-const getPlayer
Int fun(){}
Auto* ptr = fun();
A. const std::string CppGame::*pData = &CppGame::name; //name is private
B. auto pData1 = CppGame::data; auto s1 = myCppGame.*pData1(); auto pData2 = CppGame::data(); auto s2 = pCppGame->*pData2;
//pData1 类型为const std::string CppGame::*(*)(void); pData2类型为const std::string CppGame::*;
C. auto s1 = (myCppGame.*pmf)(); auto pmf2 = &CppGame::show; (myCppGame.*pmf2)();
D. auto s1 = (myCppGame.*pmfc)(); auto s2 = (cppGame.*pmfc)(); auto s3 = (cppGame.*pmf)();
//pfm是指向non-const getPlayer函数的,cppGame是const对象
//const对象不能调用非const函数
E. auto pmfs = &CppGame::getNumOfSg; auto s1 = (*pmfs)(); auto s2 = pmfs; //getNumOfSg为静态成员函数:不需要取地址符&
//pmfs 类型为 unsigned int (*)(void); s1类型为unsigned int; s2类型为unsigned int (*)(void)
F. auto pmfs2 = CppGame::getNumOfSg; auto s1 = pmfs2; auto *pmfs3 = CppGame::getNumOfSg;auto s2 = pmfs3;
//pmfs2, s1, pmfs3, s2类型都为unsigned int (*)(void);
Answer: A, D
Explanation:<<C++ primer>> chapter:19.4 or https://www.bilibili.com/video/BV1hB4y1b7uq/?spm_id_from=333.788.recommend_more_video.9

本文详细解析了C++中类成员指针的概念,包括数据成员指针和成员函数指针的声明、初始化及使用。通过实例展示了如何利用成员指针操作类的成员变量和成员函数。

1万+

被折叠的 条评论
为什么被折叠?



